Here's the code:
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 only searches the first line not the others. So it prints nothing. How can i fix this problem?
You should be looping over all the lines - and then if the line matches, then print it out (or whatever). For example:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// Now check the line, and do whatever you need.
}
Or you could still use findInLine
, just explicitly calling nextLine
as well:
while (scanner.hasNextLine()) {
if (scanner.findInLine(...)) {
...
}
// Read to the end of the line whether we found something or not
scanner.nextLine();
}
See more on this question at Stackoverflow