Search code examples
javafiledelimitermeta

Trying to read from scanner using a constant string delimiter - java.util.scanner


I've been trying to find a way to read some data from a file delimited by a constant string delimiter.

The delimiter includes meta-characters, and I'm trying to split ONLY where it appears, so regex is not suitable.

Can someone advise a way for me, I know how to implement my own method to do this, what I'm asking is whether or not there's something built-in to java to do this.

Example:

John %#@!~23s,243 doe %#@!~23s,243 went %#@!~23s,243 to market to buy some milk.

Would result in.

Call 1: .next() --> John Call 2: .next() --> doe Call 3: .next() --> went Call 4: .next() --> to market to buy some milk.


Solution

  • The delimiter includes meta-characters, and I'm trying to split ONLY where it appears, so regex is not suitable.

    You can still use a regex for the delimiter - just use Pattern.quote to make sure "special" characters in regular expressions don't interfere.

    That's assuming you still want to use Scanner, of course. You could use the Splitter class within Guava