Thursday, 1 February 2018

java - Scanner cannot read input

The problem is in this line:


int size = scan.nextInt();

When your code asks to enter the size of the matrix, you enter a number, and then hit enter. The above code, however, only reads the number that you enter. It does not consume the enter (\n character).


After your code consume the matrix size, your code does:


String[] input = scan.nextLine().split(" ");

which consumes the enter (\n character), and then split it by space. This (scan.nextLine()), of course, returns an empty string.


To fix it, instead of using:


int size = scan.nextInt();

use:


int size = scan.nextInt();
scan.nextLine();

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...