Reading a string backwards

S

Sharp

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
 
F

Fred

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?
 
Y

Yamin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top