I have text input which looks like this:
!10#6#4!3#4
I have two patterns for the two types of data found in the input above:
Pattern totalPattern = Pattern.compile("![0-9]+");
Pattern valuePattern = Pattern.compile("#[0-9]+");
I wanted to get the following output from the input above:
total value value total value
So I wrote the following code to solve this problem:
try (Scanner scanner = new Scanner(inputFile)) {
while (scanner.hasNext()) {
if (scanner.hasNext(totalPattern) {
System.out.print("total ");
scanner.next(totalPattern);
} else if (scanner.hasNext(valuePattern)) {
System.out.print("value ");
scanner.next(valuePattern);
}
}
}
But this obviously did not work because it couldn't separate the input into tokens because there are no delimiters in the input. The scanner sees the entire input as one big single token. I know I could change the input to have spaces before the #
and !
, but I'm not allowed to change the input for this problem. I tried messing with the scanner to change the delimiter but couldn't get it to work. I kind of think that the Scanner isn't the right tool for this job, but I'm not really sure what is.
Edit
As g00se pointed out, I could use !
and #
as delimiters. In the above example that wouldn't be a problem, but to change the problem slightly, suppose the input was:
!!!10#6###4!!3#4
The patterns would be:
Pattern totalPattern = Pattern.compile("[!]+[0-9]+");
Pattern valuePattern = Pattern.compile("[#]+[0-9]+");
If I wanted to use the number of !
or #
as part of the information encoded in the data, using !
and #
as delimiters doesn't work. I still think Scanner is not the right tool, I just don't know what is.
Edit 2
Okay, I realize that I didn't really write this question in the right way and I have waisted some time. Ultimately I wanted a solution that is more general purpose. The solution should take in a series of patterns, and consume the input from the front and return a series of tokens that it can break the input into. The two patters from above are just examples. I realize now though that because I didn't specify the type of solution I wanted, I got some great answers which solved the problem given above in the simplest way possible.