4

I am having this problem a lot. When I use a Scanner a lot of times, it doesn't get input from user.

Scanner scan = new Scanner(System.in);

        System.out.println("1---");

        int try1 = scan.nextInt();

        System.out.println("2---");

        int try2 = scan.nextInt();

        System.out.println("3---");

        String try3 = scan.nextLine();

        System.out.println("4---");

        String try4 = scan.nextLine();

When I run this code, result it :

1---

12

2---

321

3---

4---

aa

As you can see, it skipped at 3rd input. Why this is happening? I solve this problem by using new Scanners, sometimes I have 5-6 different Scanners and it looks so complicated. Another problem is : there is an error "Resource leak: scan is never closed". I am really confused.

berkc
  • 525
  • 3
  • 9
  • 21
  • 1
    You typed 3, then 2, then 1, then . The `int` is 321. The is still waiting to be processed. Then the first `nextLine` call reads until the next , but there's already an you typed before, so it returns that line (which contained nothing). Consider what happens if you type "123 456 abcdef" – user253751 Nov 06 '14 at 12:27
  • Dude, the warning "Resource leak: scan is never closed". You need to close your scan. scanner.close(); – Gabriel Lidenor Nov 06 '14 at 12:27

3 Answers3

9

The problem is that by using scanner.nextInt() you only read an integer value, but not the whole line and you don't consume the newline character (\n) that is appended to the line when you press Enter.

Then, when you process reading with scanner.nextLine() you consume the newline character (\n) and from the previous row and don't read the line you want to read. In order to force it to read it, you have to add an additional input.nextLine() statement.

 System.out.println("2---");
 int try2 = scan.nextInt();
 System.out.println("3---");
 scan.nextLine(); //<-- fake statement, to move the cursor on the next line
 String try3 = scan.nextLine();

Not related to the question, you have to close the Scanner, after finishing work, otherwise the compiler complains with a warning:

scan.close();
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
4

Use next API rather than nextLine as when you do nextInt you press enter and it generates number + \n and nextInt is only going to take an integer and won't take \n which in turn gets passed to next input i.e. try3 which is why it gets skipped.

        Scanner scan = new Scanner(System.in);

    System.out.println("1---");

    int try1 = scan.nextInt();

    System.out.println("2---");

    int try2 = scan.nextInt();

    System.out.println("3---");

    String try3 = scan.next();

    System.out.println("4---");

    String try4 = scan.next();
Clover
  • 507
  • 7
  • 22
SMA
  • 36,381
  • 8
  • 49
  • 73
0

Well, for the first question use

scan.next()

insted of using

scan.nextLine();

For the second question I'd recommend using try, assuming you need to close your scan as your compiler is warning: "Resource leak: scan is never closed"

Scanner scanner= null;
  try {
    scanner= new Scanner(System.in);

  }
  finally {
    if(scanner!=null)
    scanner.close();
 }
Gabriel Lidenor
  • 2,905
  • 2
  • 25
  • 26