aspose file tools*
The moose likes Beginning Java and the fly likes How do I read (right to left) from a string one character at a time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I read (right to left) from a string one character at a time?" Watch "How do I read (right to left) from a string one character at a time?" New topic
Author

How do I read (right to left) from a string one character at a time?

Rob Hopping
Greenhorn

Joined: Oct 29, 2004
Posts: 4
Example:

String "1234567"

I need to read "7" as an int and perform an operation
then I need to read "6" as an int
then "5"
then "4" etc....

Any help will be greatly appreciated

Thank you
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
If it were my assignment, I'd turn the string into an integer with Integer.parseInt() and then pick off the digits from right to left with the % and / operators until the residual value was 0.


Mike Gershman
SCJP 1.4, SCWCD in process
Alain Boucher
Ranch Hand

Joined: Feb 25, 2003
Posts: 51
One possible solution:

String t = "123456";

for (int curs=t.length()-1;curs>=0;curs--){
int g = new Integer((""+t.charAt(curs))).intValue();

System.out.println(g + "");
}


Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You could also select each character in the string with charAt() and convert it to a number with Character.digit().
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
You said:

Example:

String "1234567"

I need to read "7" as an int and perform an operation
then I need to read "6" as an int
then "5"
then "4" etc....


You might try a for loop that repeatedly calls the charAt() method of String on the String you want to process. For example:

DD


SCJP 1.4<br />SCWCD (in progress)
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hang on, that won't quite work right, will it? I need to convert the ASCII value in the char to an integer, like this:


Sorry about that!
DD
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
I think what Mike suggested earlier is good.

"If it were my assignment, I'd turn the string into an integer with Integer.parseInt() and then pick off the digits from right to left with the % and / operators until the residual value was 0. "

That would take care of any leading zero's or spaces in the string.

You can take the input number and % and / by 10 and then do whatever you want.
Rob Hopping
Greenhorn

Joined: Oct 29, 2004
Posts: 4
I must be missing something here.

This is my first programming class and the assignment is to develop a method to convert a binary value (11010011) to a decimal value (211).

I'm not sure what the best way to proceed is. I know the steps to do the conversion. I'm just not sure how to get JAVA to read one digit at a time.

Thanks again for your help.
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
This is what a number of us were trying to suggest to you with the code we posted above.
You can think of String objects in Java as a sort of array: starting from the leftmost position, this is equivalent to the element at the first position in a Java array, with the index numbering starting from zero.

Thus in your string "1234567",
character at index 0 is 1
character at index 1 is 2
character at index 2 is 3

... and so on up to the end of the string.

To find out the size of the String, and thus the size of the string "array", you can call the String.length() method on the String, to return an integer defining its length, i.e
(number of characters in String) - 1, since numbering starts at index zero.

Thus to return the character at a particular index, you call the charAt()
method on the string, thus myString.charAt(2), will return the char representation of the character at index 2, which in the case of the string
"1234567" is the Unicode value corresponding to the character "3".
So to go from right to left, you can use a for loop:
for (int i = (myString.length()-1); i >= 0; i--)
this tells the JVM:
"initialise the int primitive i to the length of the string myString -1,
i.e. the position of the rightmost character. Then, for as long as i is
greater than or equal to zero, execute the expressions in the for loop, decrementing i by 1 after each pass through the loop".
You can then read the characters one at a time from right to left on each pass through the for loop, convert them to integers using one of the methods described in the postings above, and use them to build up the value of your decimal total.
OK?

DD
Rob Hopping
Greenhorn

Joined: Oct 29, 2004
Posts: 4
You guys (and Gals) rock.

I think I understand what I need to do now. Thanks for all your help

Rob
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
StringCharacterIterator can traverse a String in both directions.

Converting binary String to int can be done by Integer.parseInt(String s, int radix) where radix is 2.
Monica Crews
Greenhorn

Joined: Oct 30, 2004
Posts: 2
I am working on a similar program in that I must read a number input by the user and give a count of the number of odd, even and zero digits. Will the tips given in this thread work for that? I have been trying it and can't get it to work. Any suggestions?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Monica,

You could use the % and / operators as suggested by Mike and Rahul.

For example:


Joyce
[ October 30, 2004: Message edited by: Joyce Lee ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923



http://home.arcor.de/hirnstrom/bewerbung
Monica Crews
Greenhorn

Joined: Oct 30, 2004
Posts: 2
Thanks for the help everyone. It has been very beneficial.

Monica
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I read (right to left) from a string one character at a time?