Scanner Sc = new Scanner(new File("Input.bin"));
String s = Sc.nextLine();
fsize = Integer.parseInt(s); // Reads 4
s = Sc.nextLine();
int mapSize = Integer.parseInt(s); // Reads 3
for (int i = 0; i < mapSize; i++)
{
byte value = 0;
value = Sc.nextByte(); // Here it is throwing the exception it should have
// print the ascii of 'b' which is 98????
String key = Sc.nextLine();
key = key.trim();
dcMap.put(key, (char)value);
// System.out.println(key + " " + (char)value);
}
Input.bin 文件内容:
4
3
b 0
c 10
a 11
最佳答案
java.util.Scanner
用于扫描文本。
因此,当您调用 Scanner#nextByte()
时,它真正希望找到的是数字的文本表示。例如,如果您在那里使用“98”而不是“b”,它会将 98 读入该字节变量。
关于Java-Scanner 不适用于 nextByte(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11153341/