Problem JAVA input throws error

0

Practicing a bit of Java I found an exercise that I found interesting to handle functions, the statement is quite long and is in a book (therefore it is difficult for me to copy it, plus I did a variation) but basically it is a matrix of 4 courses and 3 classes which each of the spaces represents how many students are in the combination, for example in the course 1 class 2 there are 0 students, or in the course 1 class 1 there are 3 students.

I have a function to add students but I do not know why it throws me an error, since I'm new to Java maybe I'm making an error that I do not perceive, the error runs me when in the insert and delete functions I ask the class that the user wants (delete | insert).

The exception that runs me is the following:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at Main.Punto_23_Pagina_92.insertar(Punto_23_Pagina_92.java:55)
at Main.Punto_23_Pagina_92.main(Punto_23_Pagina_92.java:25)

I enclose the program code, thank you very much in advance, I will be watching the comments.

package Main;

import java.util.Scanner;

public class Punto_23_Pagina_92 {

    public static void main(String[] args) {
        Scanner scanner;
        scanner = new Scanner(System.in);

        int[][] cursos = new int[4][3];

        for (int i = 0;i<4;i++) {
            for (int i2 = 0;i2<3;i2++) {
                cursos[i][i2] = 0;
            }
        }

        System.out.println("(1) Añadir alumnos - (2) Eliminar alumnos - (3) Alumnos en clase - (4) Salir");
        int opcion = scanner.nextInt();
        char clase = 'D';

        while (opcion != 4) {
            if (opcion == 1) {
                cursos = insertar(cursos);
            } else if (opcion == 2) {
                cursos = eliminar(cursos);
            } else if (opcion == 3) {
                for (int i = 0;i<4;i++) {
                    for (int i2 = 0; i2<3; i2++) {
                        if (i2 == 0) {
                            clase = 'A';
                        } else if (i2 == 1) {
                            clase = 'B';
                        } else if (i2 == 2) {
                            clase = 'C';
                        }
                        System.out.println ("El curso "+i+" de la clase "+ clase +" tiene "+ cursos[i][i2]+" estudiantes" );
                    }
                }
            }
            System.out.println("(1) Añadir alumnos - (2) Eliminar alumnos - (3) Alumnos en clase - (4) Salir");
            opcion = scanner.nextInt();
        }

    }

    public static int[][] insertar(int[][] cursos){
        Scanner scanner;
        scanner = new Scanner(System.in);
        System.out.println("Elige el curso:");
        int curso = scanner.nextInt();
        System.out.println("Elige la clase:");
        char clase;
        clase = scanner.nextLine().charAt(0);
        System.out.println("¿Cuantos estudiantes van a ingresar al curso?:");
        int cantidad = scanner.nextInt();
        if (clase == 'A') {
            cursos[curso][0] = cursos[curso][0] + cantidad;
        }else if(clase == 'B') {
            cursos[curso][1] = cursos[curso][1] + cantidad;
        }else if (clase == 'C') {
            cursos[curso][2] = cursos[curso][2] + cantidad;
        }
        return cursos;

    }

    public static int[][] eliminar(int[][] cursos){
        Scanner scanner;
        scanner = new Scanner(System.in);
        System.out.println("Elige el curso:");
        int curso = scanner.nextInt();
        System.out.println("Elige la clase:");
        char clase = scanner.nextLine().charAt(0);
        System.out.println("¿Cuantos estudiantes van a salir del curso?:");
        int cantidad = scanner.nextInt();
        if (clase == 'A') {
            if (cursos[curso][0] - cantidad >= 0) {
            cursos[curso][0] = cursos[curso][0] - cantidad;
            } else {
                System.out.println("No puedes quitar estudiantes que no existen");
            }
        }else if(clase == 'B') {
            if (cursos[curso][1] - cantidad >= 0) {
                cursos[curso][1] = cursos[curso][1] - cantidad;
                } else {
                    System.out.println("No puedes quitar estudiantes que no existen");
                }
        }else if (clase == 'C') {
            if (cursos[curso][2] - cantidad >= 0) {
                cursos[curso][2] = cursos[curso][2] - cantidad;
                } else {
                    System.out.println("No puedes quitar estudiantes que no existen");
                }
        }
        return cursos;
    }

}
    
asked by OrlandoGK 03.01.2019 в 23:02
source

1 answer

1

The problem you are talking about is generated due to a misuse of the nextLine() method. To read char values by console, you must do it in the following way:

char clase = scanner.next().charAt(0);

Modify your delete and insert methods, and that will solve your problem. If it was helpful, mark the answer as valid to help others with a problem similar to yours. Good luck!

    
answered by 03.01.2019 / 23:58
source