作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
代码如下:
BufferedReader in= new BufferedReader(new FileReader("C:\\Users\\ASUS\\Desktop\\123.txt"));
Scanner scanner= new Scanner(in);
while((scanner.findInLine("abc"))!=null){
if(scanner.hasNext()){
System.out.println(scanner.next());
}
}
findInLine 只搜索第一行,不搜索其他行。所以它什么都不打印。 我该如何解决这个问题?
最佳答案
您应该遍历所有 行 - 然后如果行匹配,则将其打印出来(或其他)。例如:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// Now check the line, and do whatever you need.
}
或者您仍然可以使用 findInLine
,只需显式调用 nextLine
即可:
while (scanner.hasNextLine()) {
if (scanner.findInLine(...)) {
...
}
// Read to the end of the line whether we found something or not
scanner.nextLine();
}
关于java - findInLine 错误仅搜索第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21355520/
代码如下: BufferedReader in= new BufferedReader(new FileReader("C:\\Users\\ASUS\\Desktop\\123.txt")); Sc
我正在运行一个简单的扫描器来解析一个字符串,但是我发现如果经常调用我会得到 OutOfMemory 错误。此代码作为为字符串数组重复构建的对象的构造函数的一部分调用: 编辑:这是更多信息的构造函数;在
我们可以使用 findInLine() 方法的字符串模式是什么。例如,如果输入是电子邮件地址,例如 vinny.kinny@gmail.com,我如何使用 java 中的 findInLine() 方
我是一名优秀的程序员,十分优秀!