Skip to content

Instantly share code, notes, and snippets.

@SakshamInABox
Last active May 8, 2019 09:57
Show Gist options
  • Save SakshamInABox/a8a40b9069685dd7c6b06feff0cdc284 to your computer and use it in GitHub Desktop.
Save SakshamInABox/a8a40b9069685dd7c6b06feff0cdc284 to your computer and use it in GitHub Desktop.
package testinggrounds;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestingGrounds {
public static void main(String[] args) {
boolean mismatch;
do {
mismatch = false;
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Please Enter A Number: ");
int userInput = scanner.nextInt();
while (userInput < 0) {
System.out.print("Sorry Number Needs To Be Postivie. Please Try Again: ");
userInput = scanner.nextInt();
}
} catch (InputMismatchException e) {
mismatch = true;
System.out.println("Sorry The Input Needs To Be A Number. Please Try Again.");
}
} while(mismatch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment