+ Reply to Thread
Results 1 to 2 of 2

Thread: Integer reverse program

  1. #1
    FoxyCc87 is offline New Born FoxyCc87 is on a distinguished road
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Exclamation Integer reverse program

    Hey everyone..I'm trying to code a program in Java that takes an integer, reverses it and outputs the reversed integer using the modulus function. I have my code so far, pasted below, could someone take a look at it and see what is wrong with it?

    thanks a bunch!

    import java.util.Scanner;

    public class ReverseInteger
    {
    static Scanner input=new Scanner(System.in);
    public static int main (String args[])
    {

    System.out.print("Enter an integer value:");//prompts user to enter an integer value
    int userValue = 0;//variable that holds users' input
    userValue=input.nextInt();//sets the user input

    System.out.print("The reversed number is:" +userValue);

    //Method Body

    int inputInt = 0;
    int reversedInt=0;
    int tempInt=0;

    while(inputInt>0){
    tempInt=inputInt%10;
    inputInt=inputInt/10;
    reversedInt=(reversedInt*10)+tempInt;
    Math.abs(inputInt);
    }//end Method body
    return reversedInt;


    }

    }
    ;)

  2. #2
    juddster is offline Toddler juddster is on a distinguished road
    Join Date
    Nov 2007
    Posts
    29
    Rep Power
    0

    Re: Integer reverse program

    Quote FoxyCc87 originally posted: View Post
    Hey everyone..I'm trying to code a program in Java that takes an integer, reverses it and outputs the reversed integer using the modulus function. I have my code so far, pasted below, could someone take a look at it and see what is wrong with it?

    thanks a bunch!

    import java.util.Scanner;

    public class ReverseInteger
    {
    static Scanner input=new Scanner(System.in);
    public static int main (String args[])
    {

    System.out.print("Enter an integer value:");//prompts user to enter an integer value
    int userValue = 0;//variable that holds users' input
    userValue=input.nextInt();//sets the user input

    System.out.print("The reversed number is:" +userValue);

    //Method Body

    int inputInt = 0;
    int reversedInt=0;
    int tempInt=0;

    while(inputInt>0){
    tempInt=inputInt%10;
    inputInt=inputInt/10;
    reversedInt=(reversedInt*10)+tempInt;
    Math.abs(inputInt);
    }//end Method body
    return reversedInt;


    }

    }
    ;)
    Firstly, your printed string will output whatever the user specified as the number and not the reversed number.

    Secondly, you never said something like inputInt = userInput; because right now...inputInt will always be 0 so it will never go into the loop.

    I quickly coded up the exact same thing (minus the reading from the command line) and it works, so here is my code:

    Code:
    public class ReverseInteger
    {
            public static void main ( String [] args )
            {
                    int toRev = 123456; /* Arbitrary number to reverse */
                    int reversed = 0;
    
                    while ( toRev > 0 ) /* Loop until the end */
                    {
                            int val = toRev % 10; /* Pop off the last digit */
                            toRev = toRev / 10; /* Shift everything over 1 digit */
                            reversed = reversed * 10 + val; /* Shift and add */
                    }
    
                    System.out.println ( reversed ); /* Output the reversed number */
            }
    }
    This code will output:
    654321

    Hope this helps,
    ~juddster

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. 2 same methods do different things
    By rumen_lazarov in forum Java Programming
    Replies: 0
    Last Post: 08-17-2009, 06:39 AM
  2. 2 same methods do different things
    By rumen_lazarov in forum General Programming
    Replies: 0
    Last Post: 08-17-2009, 06:34 AM
  3. C++ or C better program
    By ChrisBrown in forum C and C++ Programming
    Replies: 0
    Last Post: 08-28-2007, 09:47 AM
  4. Timer exercise
    By Jeriko Yepez in forum C and C++ Programming
    Replies: 9
    Last Post: 03-06-2006, 05:29 PM
  5. what does this mean --> cc++/Socket.h???
    By AsianCutie in forum C and C++ Programming
    Replies: 15
    Last Post: 06-27-2005, 12:47 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