0
import java.util.Scanner;

public class VerticalWire {
    public static void main(String[] args)
    {  
        Scanner in = new Scanner(System.in);
        System.out.println("how many wires");
        String howManyWires = in.next();

        switch(howManyWires) {
            case "3":
            {
                 System.out.println("true");
                 break;
            }

            case "3 5":
            {
                 System.out.println("false");break;}
            }
     }
}

I tested if I enter "3 5" it returns true even though I think it should return false! What is wrong?

1 Answers1

0

Use in.nextLine() instead of in.next().

Documentation of scanner.next() states that

Finds and returns the next complete token from this scanner.

So when you enter 3 5 as input, it will read only 3 when you call next method and if you again call next method then it will read 5.

Naman Gala
  • 4,480
  • 1
  • 18
  • 48