Displaying Multiple Lines that can hold and accept inputs at the same time while at runtime with one scanner

Viewed 81

I'm using do while loop

public class Program {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    float grades;
    int subj,i = 1;
    System.out.printf(" Enter how many subjects: ");
    subj = input.nextInt();
   do {
     System.out.printf("\nEnter Grade [%d]: ", i);
     i++;
   }while(i <= subj);
    grades = input.nextInt();

   } 
}

Output

    Enter how many subjects: 5

Enter Grade [1]: can't go back here 
Enter Grade [2]: can't go back here
Enter Grade [3]: can't go back here
Enter Grade [4]: can't go back here
Enter Grade [5]: I'm automatically here right after I run the project

Being new isn't an excuse so I did a research but most of them are one way.

I want to display multiple lines that can hold and accept inputs at the same time in runtime, it's like converting the lines into fields that can be edited or changed while at runtime.

Edit[1]: IDE is NetBeans 8.2

Edit[2]: I already have a working program of this (which is one way), it just came up to my mind and I kinda want to upgrade it.

1 Answers

The issue not that it is skipping over reads, but that it is not reading during the loop. If you want to read from the input for each time the loop runs, you need to put the next inside of the loop. On top of that, if you just have a single float that contains the grades, that variable will be overridden whenever a new grade is received. This can be fixed by adding an array of floats in place of the standard float. The code would look something like this:

public class Program {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int subj,i = 0;
        System.out.printf(" Enter how many subjects: ");
        subj = input.nextInt();
        float[] grades = new float[subj];
        do {
            System.out.printf("\nEnter Grade [%d]: ", i+1);
            grades[i] = input.nextInt();
            i++;
        }while(i < subj);
    } 
}

(A few other edits were made to this)

So, here is what it does now, line by line. The first 2 lines of the method are just instantiating variables to use in the method. i is set to 0, to allow for easier editing of the array. The next two are getting your input number of subjects. Pretty standard. The next line, the one setting grades is creating a new array of floats for however many subjects. Then, the loop runs. In the loop, the first line is printing, with i+1 to correspond with the subject number rather than the index of the array. The line after that will put the inputted grade in the array, at the index i, and the line after that increments i. After that, the loop check is checking if the index of i is less than the number of subjects. This works, because i starts at 0, and when it is 5, it just inserted the 4th element in the list, which is the last element.

Note: Arrays start at 0, so that is why i is instantiated at 0, and why the other instances of checking i are changed. They are compensating for this change.

Related