4

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.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • The reason why you can't find a solution is because there isn't one, at least not one that uses a standards Scanner. You can possibly solve this using a 3rd party Scanner or a GUI library such as Swing or the newer JavaFX. – Hovercraft Full Of Eels Sep 09 '18 at 15:29
  • 2
    The "standard" way to create a TUI ([Text User Interface](https://en.wikipedia.org/wiki/Text-based_user_interface)) is with a library called [curses](https://en.wikipedia.org/wiki/Curses_(programming_library)): https://stackoverflow.com/a/20452886/2970947 – Elliott Frisch Sep 09 '18 at 15:38
  • Yep, @ElliottFrisch gave a link about one of the 3rd party console libraries that can work well for you. 1+ to the answer – Hovercraft Full Of Eels Sep 09 '18 at 15:43
  • Thanks to both of you. – iMadeThisToAsk Sep 09 '18 at 15:54
  • Do you really want to be able to go back and edit previous entries, or do you just want to capture the grade for each subject? If you want to edit entries you'll need a TUI, and some great links have been provided for that. If you just want to capture the grade for each subject you need to put `input.nextInt()` inside the do/while loop, and store the input in an array. – RaffleBuffle Sep 09 '18 at 15:55
  • @SirRaffleBuffle Yes, I want to, 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. – iMadeThisToAsk Sep 09 '18 at 16:08
  • What IDE are you using? – 0xCursor Sep 09 '18 at 16:19
  • @LAD I'm using NetBeans 8.2 – iMadeThisToAsk Sep 09 '18 at 16:22
  • Do you have a requirement about your program must be console application? If that is not the case you can use Java Swing API and create one main window and put some TextFields onto it. User can write edit and save via these textFields. Here you can find some usefull info about [JTextField](https://www.codejava.net/java-se/swing/jtextfield-basic-tutorial-and-examples) – Akiner Alkan Jan 10 '19 at 14:20

2 Answers2

0

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.

Fishy
  • 1,275
  • 1
  • 12
  • 26
0
do {
     System.out.printf("\nEnter Grade [%d]: ", i);
     i++;
   }while(i <= subj);

You are not scanning any inputs inside the loop. you are only displaying a text and incrementing i.
And when you finally ask for the input outside the loop, you have already printed all the lines asking the user for grades and the value of i has already exceeded the number of subjects.

Senozoid
  • 11
  • 2