0

If I run this code

Scanner sc = new Scanner();
while (true) {
        if (sc.next().equals("1"))
            System.out.println("--1--");

        else if (sc.next().equals("2"))
            System.out.println("--2--");

        else if (sc.next().equals("3"))
            System.out.println("--3--");

        else if (sc.next().equals("4"))
            System.out.println("--4--");

        else if (sc.next().equals("help"))
            System.out.println("--help--");
    }

It will not read the first time I type enter. I have to type 2-4 times before it reads the input. A session could look like this:

1
1
1
1
--1--
3
3
--3--
help
2
1
help
--help--

No matter what I type, it will only read the last input of the four inputs. Sometimes it reads after two inputs. I'm really confused about this. Should I instead use multiple scanners?

yeahboy
  • 71
  • 10

3 Answers3

3

Your concepts are wrong here.

Each time you ask for sc.next() it will wait for the input. If that input is equal to what you want it to be, then the code is executed.

You can correct this by storing sc.next() in a String variable, and then comparing it.

Here: if (sc.next().equals("1")) it asks for an input.

If that input is 1 then the code is executed and --1-- is printed out. Else, it jumps to this: if (sc.next().equals("2")). Now if the input is 2 then the code to print --2-- is executed. Else, it jumps to if (sc.next().equals("3")) and so on.

You can correct this by:

  • storing sc.next() in a String variable, and then comparing it.
  • using a switch-case block to compare the input.
dryairship
  • 5,702
  • 2
  • 27
  • 52
1

You're calling sc.next() multiple times - so if the input isn't 1, it's going to wait for more input to see whether the next input is 2, etc. Each call to sc.next() will wait for more input. It doesn't have any idea of "that isn't the input you were looking for, so I'll return the same value next time you call".

Use a local variable to store the result of sc.next()

while (true) {
    String next = sc.next();
    if (next.equals("1"))
        System.out.println("--1--");
    else if (next.equals("2"))
        System.out.println("--2--");
    else if (next.equals("3"))
        System.out.println("--3--");
    else if (next.equals("4"))
        System.out.println("--4--");
    else if (next.equals("help"))
        System.out.println("--help--");
}

Also consider using a switch statement instead...

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • This fixed it, and yes, I will use switch instead. Thanks! – yeahboy Mar 23 '16 at 11:55
  • @yeahboy when do you type help? Show us your new code. I guess you are having a problem similar to this: http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods] – dryairship Mar 23 '16 at 12:11
0

You are calling sc.next() multiple times

Solution code :

        Scanner scanner = new Scanner(System.in);
        while(true){
            switch (scanner.next()) {
                case "1":
                    System.out.println("--1--");
                    break;
                case "2":
                    System.out.println("--2--");
                    break;
                case "3":
                    System.out.println("--3--");
                    break;
                case "4":
                    System.out.println("--4--");
                    break;
                case "help":
                    System.out.println("--help--");
                    break;
                default:
                    break;
            }
        }
koolkoda
  • 355
  • 2
  • 5