Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner skip

 
Brian Woodring
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Woodring
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, that's not it. Still need help.
 
Brian Woodring
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Woodring
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That put me on the right track - thanks!
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case it's helpful, there's a good regex tester here:

http://www.fileformat.info/tool/regex.htm
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic