java - 使用扫描仪和文本文件获取一行中的某个字符串

标签 java

所以我希望扫描仪分析文本文件的每一行,并在 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/

相关文章:

java - "Curl -F"Java 等价物

java - 让Gson按照一定的顺序序列化Set

java - 如何通过 SMPP 协议(protocol)连接到 SMSC 服务器?

java - gcm_defaultSenderId 无法解析

java - 使用 Hibernate-JPA 更新父子关系时出错

java - 运行从另一个文件夹调用类的 Java 类

java - 推荐的框架、库和概念

java - 循环 GC 在映射中起作用吗?

java - 类 : interface org. testng.annotations 的重复注释。之前方法

java - 使用 MockitoJUnitRunner.class 而不是 SpringJUnit4ClassRunner.class