Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add to Arraylist and show it in Java?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading