Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Method to reverse String using While Loop

  1. #1
    Junior Member Montrell79's Avatar
    Join Date
    Feb 2012
    Location
    Minneapolis, MN
    Posts
    9
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool Method to reverse String using While Loop

    Hello, I am trying to create a program that will output the reverse of any string of integers. I got the program to work with some help from another forum, but I'm just not understanding why it works.
    Specifically, what purpose is the + 10 * result serving?
    I can tell that it is allowing result to store all the modulus results instead of just replacing them as the loops continues, but I want to understand why.
    I've tested the program without the +10*result, and it doesn't work because the return result will only store the last result. For example, if you enter 654, instead of getting 456, you would only get 4 since result keeps getting replaced by 6, 5 & 4.

    Here is the code: I appreciate any feedback or help!

    public static void main(String[] args) {
    String num1Str;
    int num1, num2;


    num1Str = JOptionPane.showInputDialog("Enter a positive integer");
    num1 = Integer.parseInt(num1Str);

    //call method
    num2 = reverse(num1);


    JOptionPane.showMessageDialog(null, "The reversed integer for " + num1 + " is " + num2);
    }


    public static int reverse(int myInput){

    int result = 0 ;
    while ( myInput > 0 )
    {
    result = myInput % 10 + 10 * result;
    myInput /= 10 ;
    }
    return result ;

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Method to reverse String using While Loop

    what purpose is the 10 * result
    Look at the value of result and the value of result*10. The second one has all the digits shifted left 1 decimal digit and a 0 added on the right.

    Think about how you would build a number given a bunch of digits and a power of 10 for each.
    2*1000 + 3 * 100 + 4 * 10 + 5
    What if I give you the numbers one by one: 2 and then 3 and then 4 and then 5. what is the result
    Now what if there are only 2 numbers: 2 and 3 and I give them to you one at a time.
    Now suppose there are 8 numbers one at a time.
    etc
    Each new number is the old*10 plus the new number.
    Last edited by Norm; February 22nd, 2012 at 03:52 PM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Montrell79 (February 22nd, 2012)

  4. #3
    Junior Member Montrell79's Avatar
    Join Date
    Feb 2012
    Location
    Minneapolis, MN
    Posts
    9
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method to reverse String using While Loop

    Thanks Norm! Got it now

Similar Threads

  1. Reverse every pair of words in a string
    By mulligan252 in forum Collections and Generics
    Replies: 4
    Last Post: October 11th, 2011, 04:29 PM
  2. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  3. How to reverse a string, skiping numbers , and ' ?
    By mlotfi in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 26th, 2011, 05:13 AM
  4. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM
  5. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM