我无法从简单的文本文件中读取,似乎无法弄清楚原因.我以前做过这个,我不确定问题是什么.任何帮助,将不胜感激!
import java.io.File;
import java.util.Scanner;
public class CS2110TokenReader {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File theFile = new File("data1.txt");
Scanner scnFile = new Scanner(theFile);
try {
scnFile = new Scanner(theFile);
} catch (Exception e) {
System.exit(1);
}
while (theFile.hasNext()) {
String s1 = theFile.next();
Double d1 = theFile.nextDouble();
System.out.println(s1 + " " + d1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method hasNext() is undefined for the type File
The method next() is undefined for the type File
The method nextDouble() is undefined for the type File
at CS2110TokenReader.main(CS2110TokenReader.java:20)
Run Code Online (Sandbox Code Playgroud)
它甚至不会扫描下一行.这是我的目标.扫描和阅读.
while (theFile.hasNext()) { // change to `scnFile.hasNext()`
String s1 = theFile.next(); // change to `scnFile.next()`
Double d1 = theFile.nextDouble(); // change to `scnFile.nextDouble()`
System.out.println(s1 + " " + d1);
}
Run Code Online (Sandbox Code Playgroud)
您正在引用Scanner类的方法File.更换theFile用scnFile在所有的调用.
其次,要调用next()和nextDouble(),但只检查hasNext()一次.这可能会让你NoSuchElementException在某个时间点.在你真正阅读它之前,确保你有一个阅读的输入.
| 归档时间: |
|
| 查看次数: |
1402 次 |
| 最近记录: |