Scanner delimiter not working as expectedwith input file (Java)

南楼画角 提交于 2021-02-07 23:43:09

问题


I am writing a program to read input from a text file that will always follow the format of char:int, such as the following:

A:3
B:1
C:2
D:2

(eof here)

I want to read in the characters and their corresponding numbers, ignoring the colons.

In my program I have the following declarations and initializations:

String fileName = "input.txt";
File file = new File(fileName);
Scanner in = new Scanner(file).useDelimiter(":");

Then, I try to read from my file using the following loop:

while(in.hasNextLine()){
        String t = in.next();
        int n = in.nextInt();
        ....
}

When I attempt to run my program in Eclipse, I receive the following error(s):

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Huffman_Encoder.main(Huffman_Encoder.java:26)

i.e., the line: int n = in.nextInt(); is throwing an exception. I am not sure why this is the case. From my understanding of Scanner delimiters, I should be skipping over the colons and so int n = in.nextInt(); should put the value following the colon into n.

What am I not seeing here?

OS is Windows 10, if that matters. Thank you.

edit: upon further testing, I've pinpointed the error as occurring immediately after String t = in.next(); Printing out t immediately after this shows 'A', and then the program throws its exception at int n = in.nextInt();


回答1:


The actual input that the scanner gets is this string:

A:3\nB:1\nC:2\nD:2

So the quick fix is to add \n to the list of delimiters:

Scanner in = new Scanner(file).useDelimiter("[:\n]");

However, this is probably not strictly correct, as this would accept any stream with colons or newlines between the letters and numbers, e.g.:

A
3:B
4

So you'd be better off reading the input line by line and splitting at the colon (see String.split()).




回答2:


Before accessing Scanner.next**() methods be advised to use the appropriate Scanner.hasNext**() methods. If you expect an integer then check the next token with Scanner.hasNextInt() and get it by Scanner.nextInt().

Here you have checked the next token with Scanner.hasNextLine() which says that there at least one more line in the input. But you are trying to access the next token with Scanner.next() and then Scanner.nextInt().



来源:https://stackoverflow.com/questions/35978417/scanner-delimiter-not-working-as-expectedwith-input-file-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!