-2

I have this problem in school, and I have no idea how to start.

Here are the directions:

Write a method with the following header to display an integer in reversed order:

public static int reverse (int number)

Example Output:

Enter an integer: 12345 in reversed order is 54321

Note:

You will need to...

  1. Find the length of the integer
  2. Extract each digit
  3. In the event that the expected outputs do not match the output tests or the code is incomplete that the program fails to run, points will be given to the sections that the codes are written properly

~~~~~~~~~~~~~~~~~~~~~~~~~

And here is the skeleton that they give me to work with:

     import java.util.Scanner;

     public class ReverseNumber {

        public static int reverse (int number) {
           // FIXME 1 (50 points): Complete the method to return the number in reversed order



        }

        public static void main (String[] args) {
           // FIXME 2 (25 points): Write the statements to prompt the user enter an integer and store it in an integer variable

           System.out.println("Enter an integer: ");


    /*      
          FIXME 3 (25 points): Write the statements to call the reverse (int number) method
        Print the result in the required format
    */      


          System.out.println("12345 in reverse order is 54321");

        }

     }

I need to know what to put in here and where to put it in order to get it to output some thing like the example. Don't change the skeleton, just add to it.

  • 1
    http://stackoverflow.com/questions/3806126/java-reverse-int-value – Reimeus Dec 04 '16 at 17:19
  • Welcome to Stack Overflow! It looks like you are asking for homework help. While we have no issues with that per se, please observe these [dos and don'ts](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845), and edit your question accordingly. – Joe C Dec 04 '16 at 17:21

1 Answers1

1

Fixme 2 and 3 are quite simple.

In Fixme 2 you need to ask the user for input, validate that what they entered is an integer, if it is store it in a variable, if it isn't ask again.

Here is a starting point for getting user input.

Fixme 3 is asking you to call the reverse(int number) giving it the number you got in fixme two. So if you saved it in a variable called myNumber, it's asking you to call reverse and pass it myNumber. Then print out whatever reverse sends back

Fixme 1 is the trickier bit.

You need to: 1) Find the length of the integer - Way to get number of digits in an int? 2) Go through the integer backwards, getting each digit in turn (use a for/next loop here) and 3) Appending them to a new integer 4) Then return the integer.

HTH

Community
  • 1
  • 1
MrB
  • 818
  • 8
  • 28