• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validate each line of a file  RSS feed

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have a file with some lines and I have to validate each line by retrieving a number in these lines.

I'm using the Scanner class to retrieve my lines, but when I need to retrieve the number (in position 80 by example), i don't have an other idea than to use the substring method, someone has a better idea? That's not very beautiful ;)

Thanks in advance!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it ALWAYS in position 80?

You can also look into tokenizing your string, which may or may not work. That would assume the data is always in the Xth token.

Or you may have to look for some other marker, like "score: ", and then get the characters after that up to some other character, like a semi-colon...

This is where programming becomes more of an art than a science...you need to analyze your data and figure out where the piece you want will be, and then determine how to get it. So I'd say first start by analyzing your data, and come up with an algorithm for finding the data you need in the line.
 
Markas Korotkovas
Greenhorn
Posts: 21
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use charAt() method of String class provided that the position is known.
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks!

An other question about Scanner, there's a way to know the previous line, the current line and the next line?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fabian Angy wrote: . . . An other question about Scanner, there's a way to know the previous line, the current line and the next line?
The only way to know the next line is clairvoyance

You are going to have to store them. You could write yourself a List class which has a fixed size and automatically deletes the oldest member. Or a Queue might do the same. There might be something already available: try Apache Commons if you can't find anything in the Sun/Oracle API. Or try a 3-member String[] array.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!