File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Scanner skip

Brian Woodring
Greenhorn

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())
{
myScanner.skip("^+[.]$");
myString += myScanner.next() + "\n";
}

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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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
Sheriff

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
sscce.org
Brian Woodring
Greenhorn

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

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

while (myScanner.hasNext("^\\+$"))
{
myString += sc.next();
}

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

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
Greenhorn

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:

http://www.fileformat.info/tool/regex.htm
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner skip