Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rob Hopping
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alain Boucher
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 + "");
}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also select each character in the string with charAt() and convert it to a number with Character.digit().
 
Dun Dagda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dun Dagda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Monica Crews
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help everyone. It has been very beneficial.

Monica
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic