Results 1 to 15 of 15

Thread: Reversing

  1. #1
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default Reversing

    how do you reverse an int, and add it to another int...

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    use the modulus operator and create a new string of the reversed int then concat, convert the second int to string, concat together and print.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    reverse an int
    what is reversing an int? What base are you using?
    If the int (in binary is) 1010 (decimal 10)
    then reversed would be 0101 (decimal 5)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    what is reversing an int? What base are you using?
    If the int (in binary is) 1010 (decimal 10)
    then reversed would be 0101 (decimal 5)
    I'm confused too.

    More specifically this process is call, bitwise complement.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Poor example. Let me expand it.
    Looking at the contents of an int in binary could be:
    000001010
    reversing the order of the bits would be
    010100000
    Not the compliment which would be:
    111110101

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    like

    int reverse = 15;

    how do i reverse int reverse to 51?

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
            int n = 15;
            String s = String.valueOf(n);
            String reverse = "";
            for(int i = s.length()-1; i >= 0; i--) {
                reverse += String.valueOf(s.charAt(i));
            }
            int reversed = Integer.parseInt(reverse);
            System.out.printf("n = %d  reversed = %d%n",
                               n, reversed);

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    How do you expect the OP to think and learn programming if you give him the code? He'll be great with select, copy and paste, but what will he do when you're not there?
    I'd think a student would get more satisfaction figuring out how to do something himself.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    How do you expect the OP to think and learn programming if you give him the code? He'll be great with select, copy and paste, but what will he do when you're not there?
    I'd think a student would get more satisfaction figuring out how to do something himself.
    I agreed with you Norm. Most of newbies in Java just looking a code, not showing any attempt themselves. I don't know why they are doing like that. Most of the time, if they not found any solution in quick time just left the forum and registered in another. For me it's too bad habit.

  11. #11
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    hahahah, well you know what lol, i am teaching myself java by trying, i've been working on figuring out how to do that for close to 2 months, and i havn't found a way, all the codes i show you or ask help with are codes i have tried for at least a month, and gotten too frusterated to keep working.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  13. #13
    whosadork is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    do you want me to post all the codes i tried>>?

  14. #14
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you don't have to post all of it. post anything that gives us the impression you actually tried to figure it out yourself first

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by xcallmejudasx View Post
    you don't have to post all of it. post anything that gives us the impression you actually tried to figure it out yourself first
    Yes, this is what I expecting. When you get the error, that message contain the line number where the error pops in your code. So you can simply post that part of the code initially with the explanation what you are going to do. If others not enough that code segment to answer your question, then post other relevant code segment.

Similar Threads

  1. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 09:42 PM
  2. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM
  3. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •