Hi
Heres the deal, i want to read a string backwards, a character at a time.
The string will be read from a file.
My approach so far is to read a line at a time using bufferedreader
readLine().
Once I have the line stored in a variable.
What is the best way to read the string backwards a character at a time.
Is there any particular class I can use that reads backwards 1 character at
a time?
I dont want to use the for loop and use charAt() method because the loop is
hardcoding,
and may may not need to read backwards all the way to the begininng.
Any help appreciated
Cheers
Sharp
Fred - 15 Mar 2005 13:22 GMT
Hello,
Might just be early, but I don't understand why it would be hardcoding.
Something like:
int len = str.length();
for( int i = len; --i >= 0; ) {
char c = str.charAt(i);
// potentially terminate early
if( condition( c ) /* some condition */ ) break;
}
no?
Yamin - 15 Mar 2005 17:05 GMT
I'm going to have to second Fred on this one and just say I'm confused
as to what you want.
So maybe you can clear it up.
Are you concerned about actually reading from the file? That is you
don't actually want to read the whole line first? If this is the case,
then you will need to tell us your file format. If its a simple text
file (that is you can edit it in a regular text editor) and you know
where in the file the string is and how long it is....then it's not too
bad...just seek to that position in the file...and read backwards.
If that is not your concern...and you're concerned about what to do
once you have the line in java String variable...the for loop, char at
is a very reasonable way to do it. But maybe you're looking for a
reverse iterator? If you are, have a look at StringCharacterIterator.