Home > Software engineering >  I'm building a program that takes an undefined number of inputs, store them in arraylists, and
I'm building a program that takes an undefined number of inputs, store them in arraylists, and

Time:07-02

I can't wrap my head around it: i call for a loop, that at the end asks the user wether they want to repeat the process. But in doing so i fundamentally reset the counter of the loop as i call the entire method, if the user wants to insert another set of data Therefore i cannot fill my array beyond the index 0, and introducing a global variable that manages the amount of times the code has been repeated doesn't help either. Here is the code:

  public class Main  {
    public static Scanner input = new Scanner(System.in);
    public static int counter = 0; 
    public static void main (String[] args) {
        inputdata();
        
    }
    public static void repeat (ArrayList<String> names, ArrayList<Integer> ages, ArrayList<Double> passinggrades) {
        System.out.println("Want to insert another student? y/n");
        String goon = input.nextLine();
        if (goon.equalsIgnoreCase("n")) {
            System.out.println("thank you for using the program. exiting...");
            showdata(names, ages, passinggrades);
            System.exit(0);
        }
        else if (goon.equalsIgnoreCase("y")) {
            
            inputdata();
        }
        else {
            System.out.println("Wrong input: "   goon   " is not y or n");
            repeat(names, ages, passinggrades);
        }
    }
    public static void inputdata () {
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<Integer> ages = new ArrayList<Integer>();
        ArrayList<Double> passinggrades = new ArrayList<Double>();
        
        for (int i = counter; i != -1; i  ) {
            System.out.print("insert name here: ");
            String name = input.nextLine();
            System.out.print("insert age here: ");
            int age = input.nextInt();
            System.out.print("insert passing grade here: ");
            double passinggrade = input.nextDouble();
            Student student = new Student(name, age, passinggrade);
            names.add(i, student.name);
            ages.add(i, student.age);
            passinggrades.add(i, student.passinggrade);
            input.nextLine();
            System.out.println(counter);
            counter   ;
            repeat(names, ages, passinggrades);
        }
    }
    public static void showdata (ArrayList<String> names, ArrayList<Integer> ages, ArrayList<Double> passinggrades) {
        System.out.print("Name |");
        System.out.print("Age |");
        System.out.print("Grade |");
        System.out.println();
        for (int i = 0; i < names.size(); i  ) {
            System.out.print(names.get(i)  " |");
            System.out.print(ages.get(i)  " |");
            System.out.print(passinggrades.get(i)  " |");
            System.out.println();
        }
    }
}

The Student class:

public class Student {
    String name;
    int age;
    double passinggrade;
    
    
    Student(String name, int age, double passinggrade){
        this.name = name;
        this.age = age;
        this.passinggrade = passinggrade;
    }
}

The error that the compiler gives when I try to insert an element beyond the first:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:756)
    at java.base/java.util.ArrayList.add(ArrayList.java:481)
    at simpleconstructor2.Main.inputdata(Main.java:42)
    at simpleconstructor2.Main.repeat(Main.java:22)
    at simpleconstructor2.Main.inputdata(Main.java:48)
    at simpleconstructor2.Main.main(Main.java:9)

CodePudding user response:

I drafted a quick version of your code for inputdata() which cuts out the need for repeat().

    ArrayList<Student> students = new ArrayList<>();  //declare and initialize your arrayList outside the loop so it is not redeclared and replaced in each iteration.
    while (true){
        System.out.print("insert name here: ");
        String name = input.nextLine();
        System.out.print("insert age here: ");
        int age = input.nextInt();
        System.out.print("insert passing grade here: ");
        double passinggrade = input.nextDouble();
        Student student = new Student(name, age, passinggrade);
        //redundant-------------------------------------
        //names.add(i, student.name);
        //ages.add(i, student.age);
        //passinggrades.add(i, student.passinggrade);
        //----------------------------------------------
        students.add(student);
        input.nextLine();

        //you can probably fit this in a boolean method that will break or return depending on value of goon
        System.out.println("Want to insert another student? y/n");
        String goon = input.nextLine();
        if (goon.equalsIgnoreCase("n")) {
            System.out.println("thank you for using the program. exiting...");
            showdata(students);  //modify your showdata() method accordingly for an ArrayList of Student
            break;
        }else if (goon.equalsIgnoreCase("y")) {
            continue;
        }else {
            System.out.println("Wrong input: "   goon   " is not y or n");
            continue;
        }
    }
  • Related