Results 1 to 6 of 6

Thread: Integers - Position

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Posts
    55

    Question 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.
    Beres

  2. #2
    Lively Member
    Join Date
    Jul 2001
    Posts
    81
    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"

  3. #3
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    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

  4. #4
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    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());
      } 
    }

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    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.

  6. #6
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    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
  •  



Click Here to Expand Forum to Full Width