0

Why does for each scan in each if{}, it prints out everything and doesn't print out wait for input and then print out the next and wait for input?

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        
        //ask if the user wants to encode or decode
        System.out.println("Type the number 1 if you want to encode OR type the number 2 to decode: ");
        int Option1 = scan.nextInt();
        
        if (Option1 == 1)
        {
            //encode
            System.out.println("Enter the text to encode: ");
            String EncodeText1 = scan.nextLine();
          //why does this skip and not wait for the first scan input?
            //shift amount
            System.out.println("Enter the shift value to encode: ");
            int EncodeShift1 = scan.nextInt();
            
            //printing out the final result
            //n/a for example
        }
        
        else if (Option1 == 2)
        {
            //decode
            System.out.println("Enter the text to the decode: ");
            String DecodeText1 = scan.nextLine();
            //same to here
            //shift amount
            System.out.println("Enter the amount to be shifted: ");
            int DecodeShift1 = scan.nextInt();
            
            //printing out the final decoded text
            //n/a for example
        }
    }
}

What can I add to make this not happen?

Type the number 1 if you want to encode OR type the number 2 to decode:
1
Enter the text to encode:
Enter the shift value to encode:
it doesn't wait for input it just prints everything out

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Jack Meng
  • 35
  • 7
  • 3
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Tim Hunter Nov 12 '20 at 21:47

2 Answers2

3

Let's say that you see the prompt

"Type the number 1 if you want to encode OR type the number 2 to decode: "

and you type 1 and then hit 'enter'. Then, what you've inputted is:

"1\n"

Two characters: a 1, and a \n (newline character).

When you do scan.nextInt(),

  1. the scanner looks at the current input stream and pulls the next integer, even if the cached input is longer than that. The next integer is 1, so the cached input is now "\n".
  2. it then calls scan.nextLine(), the scanner continues to read the cached input until it hits a newline character, which happens immediately. It then returns everything that was read up until that newline character, i.e. the empty string "".
  3. you ask it for scan.nextInt(). Since the scanner is now out of cached input, it waits for you to enter more, pausing the program until you do so.

Essentially, the scanner will continue going on whatever input you feed to System.in until it runs out, and only then will it ask for more.


The other answerer provided the solution of using EncodeText1 = scan.next(), followed probably by EncodeText1.trim() to remove the whitespace that's now at the front (and probably back). This will eat the entire rest of the scanner's cached input, and whatever you enter after that.

Another possible solution would be to read the integer as a string, and then convert it to an integer yourself - thus reading exactly one line at the same time:

int Option1 = Integer.valueOf(scan.nextLine().trim());

The main functional difference here is that scan.nextInt() and the above will give you different kinds of exceptions if something is entered that isn't an integer (InputMismatchException for what you're currently using, NumberFormatException for this potential solution).

Green Cloak Guy
  • 18,876
  • 3
  • 21
  • 38
0

Use String EncodeText1 = scan.next(); instead. .nextLine() doesn't behave as expected. It's very common to see this problem.

https://stackoverflow.com/a/64611564/7619034

Rob Evans
  • 2,476
  • 1
  • 5
  • 11