| Author |
Validate each line of a file
|
Fabian Angy
Ranch Hand
Joined: Oct 27, 2008
Posts: 72
|
|
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!
|
SCJP 5
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Markas Korotkovas
Greenhorn
Joined: Jan 27, 2011
Posts: 21
|
|
|
You could also use charAt() method of String class provided that the position is known.
|
 |
Fabian Angy
Ranch Hand
Joined: Oct 27, 2008
Posts: 72
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32604
|
|
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.
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Validate each line of a file
|
|
|