1

I have an assignment that requires me to write a program that reverses the order of a positive integer that a user enters i.e 1234 becomes 4321.

The program also has to validate that the input is a positive integer. Both of these have to be done in separate methods.

I have done the validation part which is included below. I want to put the user input in an array but I have no idea how to use the validated number in a separate method. Any help would be appreciated.

    import java.util.Scanner;

    public class order {

    public static void main(String[] args) {

        String userEntry;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter a positive integer.");
        userEntry = keyboard.nextLine();

        userEntry = validate (userEntry);
        int original = Integer.parseInt(userEntry);
        System.out.println("The original number is: "+ original);

    }

    public static String validate (String userInput) { 

        int userInputLength = userInput.length();
        int counter = 0;

        Scanner keyboard = new Scanner(System.in);

        while (userInputLength == 0) {                                                          
            System.out.println("That is not a valid integer. Try again");
            userInput = keyboard.nextLine();
            userInputLength = userInput.length();       
        }

        while (counter < userInputLength) {

            if (Character.isLetter(userInput.charAt(counter))) {

                System.out.println("That is not a valid integer. Try again");
                userInput = keyboard.nextLine();
                userInputLength = userInput.length();
                counter = 0;
            }
            else
            {
            counter++;
            }

            while (userInputLength == 0) {

                System.out.println("That is not a valid integer. Try again");
                userInput = keyboard.nextLine();
                userInputLength = userInput.length();
            }
        }
        return userInput;   
    }
iluusion
  • 19
  • 6

4 Answers4

0

You can use this method to reverse a number:

public int reverseInt(int input)
{
    long reversedNum = 0;

    long input_long = input;

    while (input_long != 0)
    {
        reversedNum = reversedNum * 10 + input_long % 10;
        input_long = input_long / 10;
    }

    if (reversedNum > Integer.MAX_VALUE || reversedNum < Integer.MIN_VALUE)
    {
        throw new IllegalArgumentException();
    }
    return (int)reversedNum;
}

FYI, Instead of your validate() method as you have written now, you can create a method to get integer value. If the inputted value is parsed successfully as integer, then it will return integer, otherwise user will get prompt again. This will reduce a lot of boilerplate:

public static int getInteger() {
    Scanner keyboard = new Scanner(System.in);
    while(true){
        try {
            return Integer.parseInt(keyboard.next());
        } catch(NumberFormatException ne) {
            System.out.print("That is not a valid integer. Try again");
        }
    }
}

And call it from main method:

public static void main(String[] args) {

    String userEntry;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter a positive integer.");
    int original = getInteger();

    System.out.println("The original number is: "+ original);
    System.out.println("The reversed number is: "+ reverse(original));

}
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
0

This is the required function:

public static int reverse (int userInpu) { 
 int lastDigit,res=0;

 while (userInpu != 0)
 {    
     lastDigit = userInpu % 10;
     if(lastDigit % 2 != 0)
     {     
            res = res * 10 + lastDigit;

     }
      userInpu = userInpu / 10; 
 }

  return res;
}
Arvindsinc2
  • 716
  • 7
  • 18
0

Simply loop through the characters and place them in the array by index (for loop). Index 0 in the input string would have an index of userInputLength - 1 - 0 in the array. The second one would have an index of userInputLength - 1 - 1. Third is userInputLength - 1 - 2. Etc.

0

Store the value returned by your validate() method and then pass it to another method in this case the reverseString() method. stringDigits is the array that has digits of the string.

In your main method

 String validatedString = validate(userInput);
 String reversedString = reverseString(validatedString);
 int[] stringInDigits = new int[userInput.length()];
 stringInDigits = getElementsAsArray(userInput);

reversedString variable will contain the reversed value.

Assuming that you will pass the original number as a String. Loop through the string in the reverse order.

Reverse String Method

   public String reverseString(String aString){
       String reverseString = "" ;
       int index = aString.length()-1;
       while(index >= 0){
           char myCharacter = aString.charAt(index);
           reverseString = reverseString.concat(""+myCharacter) ;
           index--;
       }
       return reverseString;
   }

Store Digits in Numeric String to an Array

Declare an array of size equal to string length. Loop through the string character by character and put it into an array

   public int[] getElementsAsArray(String aString){
       int[] digits = new int[aString.length()];
       for(int i=0;i<aString.length();i++){
           digits[i]=Integer.parseInt(""+aString.charAt(i));
       }
       return digits;
   }
  • Thanks for this. It does what I was looking for and also answers my question. The only problem is that I need to also need to display the even numbers and odd numbers separately, which is why I initially asked how to take the input in an array. – iluusion Apr 04 '17 at 07:20