File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Scanner skip Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner skip" Watch "Scanner skip" New topic

Scanner skip

Brian Woodring

Joined: Apr 28, 2002
Posts: 4
I want to skip a line in a text file that begins with a particular character using the Scanner skip(String pattern). I can't find an example of how this works to skip lines starting with '+':

String myString = "";
Scanner myScanner = new Scanner(file);
while (myScanner.hasNext())
myString += + "\n";

I realize there are a lot of ways to do this - but I want to use skip()
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46342
Welcome to the Ranch.

Have you tried escaping the + character. It is a metacharacter, so has to be escaped. Only the backslash probably has to be escaped too! Try changing the + to \\+ and see whether that works.
Read the regular expressions part of the Java Tutorials. It is nice and easy to understand.
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Campbell Ritchie:
...the backslash probably has to be escaped too! ...

Yes. Within a String literal, a single backslash would be interpreted as a Unicode or character escape. So in a String literal representing a regex, the double backslash is needed.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Brian Woodring

Joined: Apr 28, 2002
Posts: 4
Nope, that's not it. Still need help.
Brian Woodring

Joined: Apr 28, 2002
Posts: 4
Actually, at this point, I would like to use hasNext(pattern) as an alternative:

while (myScanner.hasNext("^\\+$"))
myString +=;

My regex's just don't seem to evaluate properly: I want to skip any line that starts with +.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
OK, so you don't know in advance if a line starts with this or not, right? And if it doesn't, you don't want to skip it. In that case you can't use skip(), because that will skip the pattern you specify, and will complain if it can't be found. I don't see a good way to use hasNext(pattern) either. Why don't you just read the line using nextLine(), and then test the string to see if it should be ignored?

Using line.startsWith("+") is simple, but if you need more flexibility, you might try something like

The ^ and $ are unnecessary here since we've already isolated a single line, and matches() requires a match of the entire string. But we need the .* too, to absorb any other characters after the +. You might further modify this to allow some optional whitespace at the beginning of the line:

Other modifications are possible, depending on your needs.

"I'm not back." - Bill Harding, Twister
Brian Woodring

Joined: Apr 28, 2002
Posts: 4
That put me on the right track - thanks!
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
Just in case it's helpful, there's a good regex tester here:
I agree. Here's the link:
subject: Scanner skip
It's not a secret anymore!