It's just a simple little method that gets user input to convert an integer from decimal to binary. It uses do-while loops to restart and verify valid input. When it catches an InputMismatchException, it starts to infinitely loop this:
Must enter a positive integer, try again.
Enter positive integer for binary conversion:
I don't know why the Scanner isn't causing the program to wait for new input when I call nextInt().
Here's the code for the method:
public static void main (String[] theArgs) {
final Scanner inputScanner = new Scanner(System.in);
boolean invalidInput = false;
boolean running = true;
int input = 0;
do {
do {
System.out.println("Enter positive integer for binary conversion:");
try {
input = inputScanner.nextInt();
if (input < 1) {
System.out.println("Must be a positive integer, try again.");
invalidInput = true;
} else {
invalidInput = false;
}
} catch (final InputMismatchException e) {
System.out.println("Must enter a positive integer, try again.");
invalidInput = true;
}
} while (invalidInput);
System.out.println(StackUtilities.decimalToBinary(input));
System.out.println("Again? Enter 'n' for no, or anything else for yes:");
if (inputScanner.next().equals("n")) {
running = false;
}
} while (running);
}