wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Return a reversed number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return a reversed number" Watch "Return a reversed number" New topic
Author

Return a reversed number

Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

Hello every one, i wrote this program that display the reverse of a number through a method called reverse and return void.
i made a string variable to hold the reversed number .
well, i want to improve this so that the method return an integer value of the reversed number.
how can i make the integer variable hold the reversed number and that can be returned to the function such as string variable i made???
i hope you help me......
thanks...


I Hear I Forget, I See I Remember, I Do I Understand.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

You'll need to change just two lines:
String stringNumber="";
stringNumber+=remainder;

The first you'll change into a declaration and initialization of an int. The initial value should be obvious.
The second line takes the current value and adds remainder to the right of it. Is there a mathematical operation or group of operations that can do this? Hint: how do you take 12 and 3 and turn it into 123?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371

How will you use same logic for String value reverse(ABCDEFG)
Stefaan Dutry
Ranch Hand

Joined: Sep 17, 2010
Posts: 32

Only a few changes need to happen indeed.

  • change the return type to int
  • declare the variable to store the result as int
  • in order to move the result 1 spot for the next digit you need to multiply it by 10 (basicaly appending a 0)
  • add the next digit
  • chop off last digit of the input


  • On a sidenote you might want to make the method do what it says, only return the reverse and not display it, that way the code is reusable and not linked to a messagedialog
    you could still call that messagedialog from within your main method in this case.

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38036
        
      22
    Stefaan Dutry , please take notice of what it says on the "beginning Java" title page:
    We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
    Please avoid giving a straight answer; you may have deprived moustafa taha of the chance to work out the answer himself, which he would have remembered much better.
    Stefaan Dutry
    Ranch Hand

    Joined: Sep 17, 2010
    Posts: 32

    Campbell Ritchie wrote:Stefaan Dutry , please take notice of what it says on the "beginning Java" title page:
    We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
    Please avoid giving a straight answer; you may have deprived moustafa taha of the chance to work out the answer himself, which he would have remembered much better.


    Sorry for this,

    I should have read it better.

    I will try not to do it again.
    xsunil kumar
    Ranch Hand

    Joined: Dec 14, 2009
    Posts: 133
    what i understood from your question is that you have one number for example 1234 and you want its reverse that is 4321.

    If yes, then just Transfer this number to char array, and write for loop starting from length of the array and goes upto 0. Concate this in different String.

    Now you have 4321 as a string. You can again get integer value using Integer.parseInt(String).

    Hope this will solve your issue.

    Sunil
    Roger Fed
    Ranch Hand

    Joined: Oct 17, 2010
    Posts: 82

    First, I'd like to thank Stefaan Dutry for his effort to help me but I'd prefer to give me the algorithm not the straight solution
    because you really solve my problem and i didn't do any thing except copying the code and running it .
    second,i agree with Campbell Ritchie for his words
    finally, I'd like to thank you all for helping me...........
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19654
        
      18

    Stefaan wrote the actual code that I hinted at. That's why we prefer to not give full answers; my hints alone probably would have you find the same answer.
    Roger Fed
    Ranch Hand

    Joined: Oct 17, 2010
    Posts: 82

    Thanks sunil very much , i tried what you say and gave me the correct answer
    here is the code and I'd be grateful if you give me your notes
     
     
    subject: Return a reversed number
     
    Similar Threads
    [Easy] Palindromes
    String return form method
    Reverse Int, char, double
    return types
    TypeCasting in Generics