I would like to be able to parse strings like the following: “123456abcd9876az45678”. The BNF is like this:
number: ? definition of an int ?
word: letter { , letter }
expression: number { , word , number }
However the class java.util.scanner doesn’t allow me to do the following:
Scanner s = new Scanner("-123456abcd9876az45678");
System.out.println(s.nextInt());
while (s.hasNext("[a-z]+")) {
System.out.println(s.next("[a-z]+"));
System.out.println(s.nextInt());
}
Ideally, this should yield:
-123456
abcd
987
az
45678
I was really hoping that java.util.Scanner would help me, but it looks like I will have to create my own scanner. Is there anything already present in the Java API to help me?
The question miss too much information. And therefore all answers are valid to the question but not to my problem.
Unfortunately you cannot use no delimiters with the Scanner class AFAIK. If you wish to ignore delimiters, you’d need to use the methods that does so such as
findInLine()orfindWithinHorizon(). In your case,findWithinHorizion()would be appropriate.