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.

2
  • 1
    You typed 3, then 2, then 1, then <enter>. The int is 321. The <enter> is still waiting to be processed. Then the first nextLine call reads until the next <enter>, but there's already an <enter> you typed before, so it returns that line (which contained nothing). Consider what happens if you type "123 456 abc<enter>def<enter>" Commented Nov 6, 2014 at 12:27
  • Dude, the warning "Resource leak: scan is never closed". You need to close your scan. scanner.close(); Commented Nov 6, 2014 at 12:27

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

Comments

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();

Comments

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();
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.