所以我希望扫描仪分析文本文件的每一行,并在 x 个单词后停止,在本例中 x=3。
我的代码看起来像这样:
scannerName.nextLine();
scannerName.next();
scannerName.next();
scannerName.next();
这里的问题是 nextLine() 使扫描器前进到当前行并返回一个字符串。因此,如果我调用 next(),它只会找到下一行的下一个字符串(对吗?)。
有办法实现我的要求吗?
谢谢
最佳答案
尝试以下代码:- while(scannerName.hasNext() != null) {
scannerName.next(); //Returns the 1st word in the line
scannerName.next(); //Returns the 2nd word in the line
scannerName.next(); //Returns the 3rd word in the line
//Analyze the word.
scannerName.nextLine();
}
关于java - 使用扫描仪和文本文件获取一行中的某个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26084417/