I do get the the task to work with BufferedReader and tried to make it work with scanner but tried many things but keep getting ExceptionInputMismatch. Can someone please explain where I go wrong?
Scanner scanner = new Scanner(System.in);
HashMap<String, Integer> map = new HashMap<>();
do {
int number = scanner.nextInt();
String name = scanner.nextLine();
map.put(name, number);
} while (!scanner.hasNextInt());
How to make it work with a scanner?
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
18 December 2020, 14:32
The Sacnner class and BufferedReader classes behave similarly, but have important differences. With the BufferedReader class you can read individual bytes or by a whole line at a time from different sources (like command line or from a file). The internal mechanism keeps track of the position of where the Reader is currently in the data that it is reading.
The Scanner class can do this as well, but also has methods for reading ints, doubles, longs, shorts, booleans, etc. Quite a bit more functionality. While this provides much greater flexibility with usage, it also takes a little understanding to use it properly. So, with your code and the Scanner class:
The nextInt() will grab all leading white space until it reaches non-whitespace, then grabs those characters until it reaches the next white space. If what it grabs is not a number then an exception will be thrown. Here is the important part: It will leave the internal cursor right at the end of the valid value that it grabbed. Some examples if you just used System.out.println(scanner.nextInt()) and typed:
"1 2 3 4 5" - the output would be 1 because that is what valid value it grabbed
" 12 " - the output would be 12, all leading whitespace is ignored and 12 is valid
"abdf 15" - this would cause an exception as "abdf" would be grabbed and that is not a valid int
"[enter][enter][enter]13[enter]" - hit enter 3 times and then type 13 and it will return 13 because enter is white space and is ignored and 13 is the valid value before the white space.
As I mentioned above, the cursor will be left at the character following the number it grabs. This is a problem because in this code it will be just prior to a new line character. The nextLine() method will grab everything from the point the cursor is at to the next new line character or end of file. That means the nextLine() method is returning a blank string in this code.
+1
Guadalupe Gagnon
18 December 2020, 14:34solution
So, to make it work with the Scanner class you could simply just use it as you would a BufferedReader by only using the nextLine() method and parsing the strings into integers; or, if you want to use the class as intended you just need to consume the new line character after reading an int, simply:
P.S. All the next methods that get specific values (boolean, int, char, decimal, etc) were designed to and behave this way. It is so you can read whole lines of values in loops without having to worry about coding to split the lines up and parsing. +3
Liliane Top Backend Developer at Procura
18 December 2020, 16:36
Wauh I'm very impressed with your explanation as I looked it up on the internet but I did not understand it truly till I read you explanation. Very much appreciated!!!
0
Liliane Top Backend Developer at Procura
18 December 2020, 16:38
Its the part that with scanner.nextInt() the cursor remains on the same line after the int. I just did not know that or did not understand what that meant.
0
Guadalupe Gagnon
18 December 2020, 16:55
=D
0