我正在尝试读取文本文件,然后使用Java中的nextInt()函数在循环中打印出整数。文本文件我的形式是:Scanner,nextInt和InputMismatchException
a 2000 2
b 3000 1
c 4000 5
d 5000 6
这里是我的代码:
public static void main(String[] args) throws FileNotFoundException {
String fileSpecified = args[0] + ".txt";
FileReader fr = new FileReader(fileSpecified);
BufferedReader br = new BufferedReader (fr);
Scanner in = new Scanner (br);
while (in.hasNextLine()) {
System.out.println ("next int = " + in.nextInt());
}
}
的错误,我总是得到的是:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
我得到我用这个错误,每次nextInt()在任何程序中。
感谢,现在它工作。 – Triple777er 2011-05-06 11:36:27