0

I am a noob in Java so far and I am having issues with this program. In this program, users can add student info and see the info. However my add function (I think does not work so I am not using it properly I guess). I realized that my error is that the index is out of boundaries, but I am not using set, I am using add so I guess it should increase the length of the arraylist isn't that right? Anyways, this is the code:

public class Student {

Scanner scan = new Scanner(System.in);
String name_s, surname_s;
int age_s, number_s;
int choice;
int index;

public void Studentt() {
    
    ArrayList<String> name = new ArrayList<String>();
    ArrayList<String> surname = new ArrayList<String>();
    ArrayList<Integer> age = new ArrayList<Integer>();
    ArrayList<Integer> number = new ArrayList<Integer>();

    System.out.println("To add student:1\nTo see student:2");
    this.choice = Integer.parseInt(scan.nextLine());

    if (choice == 1) {
        System.out.println("Name:");
        this.name_s = scan.nextLine();
        name.add(name_s);
        System.out.println("Surname:");
        this.surname_s = scan.nextLine();
        surname.add(surname_s);
        System.out.println("Age:");
        this.age_s = Integer.parseInt(scan.nextLine());
        age.add(age_s);
        System.out.println("Number");
        this.number_s = Integer.parseInt(scan.nextLine());
        number.add(number_s);
        end();
    } else if (choice == 2) {

        System.out.println("Index of the student you want to see:");
        this.index = Integer.parseInt(scan.nextLine());
        System.out.println(name.get(index));
        System.out.println(surname.get(index));
        System.out.println(age.get(index));
        System.out.println(number.get(index));
        System.out.println("Done.");
        end();
    } else {
        System.out.println("Wrong choice.");
        Studentt();

    }

}

public void end() {
    try(Scanner scan = new Scanner(System.in)){
        int menu;
        System.out.println("To repeat:1 \nExit:2");
        menu = Integer.parseInt(scan.nextLine());
        if (menu == 1) {
            Studentt();
        } else if (menu == 2) {
            System.out.println("Done.");
        } else {
            System.out.println("Wrong choice.");
            end();
        }
    }

}

and this is the output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 0
  • Hi, I guess the probleme is in xxx.get(index), it should be index-1 since arrays starts from 0 not 1. – S_intg Nov 15 '21 at 10:14
  • Exception is not thrown by `add` method but by `get` method. You are trying to access element from index that doesn't exist in the list. – Mirek Pluta Nov 15 '21 at 10:14
  • 1
    There are various other conceptual things wrong here: that "menu" code there, that actually "drives" your program (tells it what to do) ... such things do NOT go into a constructor. A constructor is supposed to initialize the fields of your class. It does NOTHING else. If you want to have such a MENU and ask for commands, put that in your MAIN method. – GhostCat Nov 15 '21 at 10:34
  • 2
    Then: you are getting OO wrong. When your Student class has those different fields already, then you do NOT create a list for each of the fields of that class. Instead, you create Student objects. Your main method keeps ONE list with Student objects. You can enter the details for a student, a new object is created, and that new object gets added to your list. – GhostCat Nov 15 '21 at 10:35
  • Finally: follow java naming conventions: dont use "_" unless in SOME_CONSTANT. And dont use abbreviations. Just go `class Student ... { private final String name; ... ` no "_s" that is just noisy clutter. – GhostCat Nov 15 '21 at 10:36

1 Answers1

0

Your problem is that if choice == 2, you want to read a specific index from the list. Before you just do '.get', you should check if the list is big enough.

If the size of your list is 1, it means that there is only one element in the list, so there is ONLY one element at index 0 (all indices start with 0).

Also, I think it is a mistake to reinitialize the lists in the method. When initializing the lists, all lists are created again and thus emptied. You can fix this by creating and initializing the lists globally (global means in the class and outside each method).

Greetings

WannaBe
  • 81
  • 7