Search code examples
javaloopsforeachrecordprompt

Java - record multiple user inputs per another input


This program should ask for a student name. Then, for each student, four grades will be recorded. Then iterate again until the user enters "C" to exit loop. Then print the results. It only shows the last four elements entered. I think my for loop isn't going to accomplish what I want. Do I need .forEach perhaps? After this, I also must sum up each four numbers per student separately. Must I have a different arrayList per student? Here's what I have so far:

package arrayList;
import java.util.Scanner;
import java.util.ArrayList;

public class TestGrades {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
ArrayList<String> studentName = new ArrayList<String>(); 
boolean loop = true;
while (loop) {
  System.out.println(" Please Enter Student Name");
  sc.nextLine();

      String student = sc.nextLine();

        if(student.equals("C"))
        {
          break;    
        }
        else
        {
          studentName.add(student);
        }


        ArrayList<Double> studentGrade = new ArrayList<Double>();
    System.out.println("Please enter Student Grade");
    for (int j = 0; j < 4; j++) {

        Double grade = sc.nextDouble();
        studentGrade.add(grade);  
    }    
    System.out.println(studentName);
    System.out.print(studentGrade);
    }
}
}

I want to print the elements of each arrayList in order of entry, first studentName then studentGrade. Would .forEach help?


Solution

  • For starters I would suggest to get the users input using the Console class. Also:

    1. The first time you are calling sc.nextLine(), you are getting input from the user and never store it.
    2. You should initialize your ArrayList<Double> studentGrade out of the loop. Every time you are looping for input it creates a new list and you are loosing the student grades of the previous loop.

    Run this, if you have to change to Scanner again, use Scanner.

    import java.util.Scanner;
    import java.util.ArrayList;
    import java.io.Console;
    
    public class Test {
    
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    ArrayList<String> studentName = new ArrayList<String>(); 
    ArrayList<Double> studentGrade = new ArrayList<Double>();
    boolean loop = true;
    
    while (loop) {
    
        System.out.println(" Please Enter Student Name");
        String student = scanner.nextLine();
    
            if(student.equals("C"))
            {
              break;    
            }
            else
            {
              studentName.add(student);
            }
    
    
        System.out.println("Please enter Student Grade");
        for (int j = 0; j < 4; j++) {
    
            Double grade = Double.parseDouble(scanner.nextLine());
            studentGrade.add(grade);  
        }    
    
        System.out.println(studentName);
        System.out.print(studentGrade);
      }
    }
    }