Thursday, February 1, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...