• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting int to string W/OUT toString, etc.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Cain Silverbane
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic