解决了,但我的声誉太低了,无法回答我自己的Q.
扫描仪输入; 扫描仪lineScanner;
input = new Scanner(System.in);
lineScanner = new Scanner(line);
String line;
char x;
askUserForInputFile(); //user selects input txt file. Method definitely works.
do {
line = input.nextLine();
if (line.length() < 6)
continue;
out.printf("debug0\n%s", line); //<----problem
do {
out.print("debug1\n");
x = lineScanner.next().charAt(0); //<-----problem
out.print("debug2\n");
//more code
我想从文件中逐行阅读并仅在其长度()=&gt;处理它时6.怎么了?
console:
debug0
Exception in thread "main" <scanned line, displayed correctly>debug
java.util.NoSuchElementException
答案 0 :(得分:0)
您需要在每次迭代开始时使用lineScanner
重置lineScanner = new Scanner(line);
。它目前正试图从你的第一行读入。
答案 1 :(得分:0)
也许你想要这样的东西
public static File askUserForInputFile() throws IOException {
System.out.println("Enter a file name: ");
String filename = input.nextLine();
return new File(fileName);
}
使用此处的方法
Scanner input = new Scanner(System.in);
File file = askUserForInputFile();
input = new Scanner(file);
Scanner lineScanner;
String line;
char x;
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.length() < 6)
out.printf("debug0\n%s", line);
else {
out.print("debug1\n");
x = lineScanner.next().charAt(0); //<-----problem
out.print("debug2\n");
}
}