|
-
Sep 12th, 2002, 04:25 PM
#1
Thread Starter
Member
Integers - Position
I have an integer called "numb" which has the value of 155.
Is there a way to split the integer up like you can with strings? With strings you can have substrings. Can I do the same thing with an integer? I need to write a recursive method that writes the value backwords. Any help you can offer is appreciated.
Thanks.
-
Sep 12th, 2002, 05:13 PM
#2
Lively Member
If you have a string reversing function already, you can convert a number to a string by simply appending an empty string, ie...
Code:
String s = 155 + ""; // s = "155"
-
Sep 12th, 2002, 08:57 PM
#3
Hyperactive Member
or:
Code:
String s = Integer.toString (numb);
and then proceed as before
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Sep 13th, 2002, 01:11 PM
#4
Hyperactive Member
Well you could cheat and use StringBuffer
Code:
public class TestString{
public static void main(String args[]){
StringBuffer buf = new StringBuffer("Beres");
System.out.println(buf.reverse());
}
}
-
Sep 13th, 2002, 01:14 PM
#5
Hyperactive Member
or you could also just write one yourself
Code:
public String reverseRecurse(String inStr) {
String outStr = new String();
if (inStr.length() > 0 ) {
outStr = inStr.charAt(inStr.length()-1) + reverseRecurse (inStr.substring(0, inStr.length()-1));
}
return output;
}
i dont have java at work to test this but I think it should work beautifully.
-
Sep 13th, 2002, 01:24 PM
#6
Hyperactive Member
also, you may want to do this mathematically. It should have better performance for you. This could be done by taking the modulus of the number and using division, but I will let you think of this one, hate to have the teacher think you know everything 
bill
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|