jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Converting int to string W/OUT toString, etc. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting int to string W/OUT toString, etc." Watch "Converting int to string W/OUT toString, etc." New topic
Author

Converting int to string W/OUT toString, etc.

Cain Silverbane
Greenhorn

Joined: Jul 22, 2002
Posts: 13
Ok. Here is the idea. We are to make a main method that calls upon a method (that accepts an int and returns a string). The method cannot use simple methods such as toString or the such-- we have to do the conversion 'manually'. Well, I had it 'semi-working'. The program would take the int, return the ASCII code equivalent ('0' = 48, '1' = 49, etc.), and it would be in reverse order... any ideas? I will post the code below just because it is really short (for the method-- main just takes an int and sends it, gets it back from the method and displays it with a few tests)
//here it is
public static String myIntToString(int num)
{
String stringNum = "";
int tempNum = num;
int lastDigit;
do
{
lastDigit = tempNum % 10;
stringNum = stringNum + (char)(lastDigit + 48);
tempNum = tempNum / 10;
}
while (tempNum != 0);
return stringNum;
}


~ Silverbane ~
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Don't you just hate those assignments that deny you the right to use standard library methods? Grrr... Anyway, you're pretty close. You have two possible strategies:
(1) Take digits from the right side of the number, convert them to characters, and add the characters to the left side of your new string.
(2) Take digits from the left side of the number, convert them to characters, and add the characters to the right side of your new string.
Right now it looks like you're mixing the two.


"I'm not back." - Bill Harding, Twister
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
There are a lot of solutions.
If you had built your string using a StringBuffer then you could just use the reverse method of the StringBuffer class.
If you had built a char array instead of a String, then you could use a for loop to reverse the order of the characters. That's actually how the StringBuffer.reverse method works.
You could parse your input parameter into an array of ints and then use a loop to parse the array of ints into an array of chars. If you parse the int array in reverse order, then your char array will be in the correct order. Then pass your array of chars to the String constructor to generate your String.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Cain Silverbane
Greenhorn

Joined: Jul 22, 2002
Posts: 13
Wahoo! You people are genius! *grins* And although it is basically a solution, I like that you didn't just actually show me what to do (ie. change the code for me) because it helps me learn. Someone tried explaining it to me earlier today and I just didn't know what they meant... be sure that I will be posting again, and for HELP, not handouts.
Anyway, I think I got it now... I just did a lil editing...

Figured that way the - numbers would work and not give wierd ascii symbols. Once again, I thank you!

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 01, 2002: Message edited by: Dirk Schreckmann ]
 
GeeCON Prague 2014
 
subject: Converting int to string W/OUT toString, etc.