I am parsing a text file that contains some text as shown below:
Level1
Some-text-here
Level2
Some-text-here
Level3
Some-text-here
I have implemented the code where it parses the file and tries to count the number of occurrences of “Level” in the file. This is how I am doing it:
Scanner scanner = new Scanner(new File(inPath));
Pattern p = Pattern.compile("Level");
while(scanner.hasNext()) {
if(scanner.hasNext(p)) {
++lastLevelCreatedWas;
}
scanner.next();
}
However the ‘if’ condition never gets hit because hasNext(p) is trying to find the exact match of “Level” and would just ignore the words “Level1”, “Level2” and so on. How am I supposed to make it consider any word that contains the keyword “Level” in it?
I think you’re looking for Scanner.useDelimiter
Try this :
It while go to the next “Level” token as you want.