0

This is probably a really stupid mistake, but I just cant seem to figure it out. When the user puts the choice as 1 to add data, it should pop up as data> then they insert their data. However, it posts data>data> showing that the loop is running twice, but I do not understand how it could run twice without putting the input in to make it run again. The last part of the code is too break the loop for when the user is done putting input in they write done.

while (true) {
        System.out.print("Select choice: "); 

        choice = s.nextInt();

        if (choice == 1) {
            inner: for (i = 0; i < 100; i++, count2++) {
                System.out.print("data>");
                line[i] = s.nextLine();
                if (line[i].equals("done")) {
                    break inner;
                }
            }
        }
Krysiak
  • 9
  • 3

1 Answers1

1

The issue is that choice = s.nextInt(); leaves a trailing new-line that nextLine() then reads (as an empty String). Add a nextLine() call like,

choice = s.nextInt(); // <-- reads int value only.
s.nextLine(); // <-- consume trailing newline.
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226