Results 1 to 13 of 13
Like Tree3Likes
  • 1 Post By eRaaaa
  • 1 Post By Norm
  • 1 Post By JosAH

Thread: reversing digits

  1. #1
    mamoonrizwan is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default reversing digits

    how do we reverse the digits .
    like if i were given 12345
    and i were to convert it to 54321.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    There are many ways. You have no idea? And do you have/(want the result) the digit as an integer or as a string?
    JeffGrigg likes this.

  3. #3
    mamoonrizwan is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default reversong digits

    integer.
    actuallu i wanna make GUI reverse number program

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    System.out.println(new StringBuilder(String.valueOf(12345)).reverse());
    or use the mathematic :)
    Java Code:
    				int digit = 12345;
    				while (digit != 0) {
    					int tmp = digit % 10;
    					digit /= 10;
    					System.out.print(tmp);
    				}
    or or or....no own further ideas? ;(

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    The GUI to get the number and show the reverse is one thing to code.
    The code to do the reversing is another thing.
    They can be done separately.
    In the example you posted, does it make any difference if the characters are numeric digits: 0-9 or can they be anything like letters or special characters: A, b, { etc

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    What should be the reverse of, say, 100? 001? or 1?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    mamoonrizwan is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default

    could you provide me with the code

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    No, but we will help you get your code working.
    What do you have now? What is your algorithm or design for solving this problem?
    JeffGrigg likes this.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mamoonrizwan View Post
    could you provide me with the code
    What would be the use of that? We give you the code; you turn it in as if you wrote it; you get a fine grade; you get a job somewhere and we end up having to do your work because you know zilch about programming. capiche? It would be much better if you give it a try on your own; if you get stuck come back here and we'll try to help you out.

    kind regards,

    Jos
    sunde887 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Do it via the string route.

    Psuedo code.

    Declare String initializing it with int // hint Integer class
    Declare temp String
    Loop through String length in reverse
    Concatenate temp with charAt


    The temp String will be the reverse of the int String.
    Last edited by stchman; 08-12-2011 at 08:05 AM.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stchman View Post
    Do it via the string route.

    Psuedo code.

    Declare String initializing it with int // hint Integer class
    Declare temp String
    Loop through String length in reverse
    Concatenate temp with charAt

    The temp String will be the reverse of the int String.
    If you want to do it with Strings (reversing 100 yields 001) don't do it the clumsy way; read what the StringBuilder class can do for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If you want to do it with Strings (reversing 100 yields 001) don't do it the clumsy way; read what the StringBuilder class can do for you.

    kind regards,

    Jos
    I forgot the include Integer.parseInt( temp ), that would remove the leading zeroes.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stchman View Post
    I forgot the include Integer.parseInt( temp ), that would remove the leading zeroes.
    That's not what I mean; the StringBuilder class has a reverse() method so no 'manual' String reversal is needed. Read its API documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Reversing a Stack
    By Ciwan in forum New To Java
    Replies: 20
    Last Post: 06-30-2011, 02:16 AM
  2. Replies: 1
    Last Post: 01-19-2011, 09:23 PM
  3. reversing digits
    By gandalf5166 in forum New To Java
    Replies: 3
    Last Post: 03-06-2010, 04:34 PM
  4. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 04:29 AM
  5. 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
  •