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;
}