I have a question, How can i do so that all input that is not a number int is ignored.
for example:
option 1(add) "Enter two integers: " input: asdf (<--asking for this to be ignored) input: 1 input: 2 output: 3
I believe that this validation would be done inside the case switch, and ive done some googling about parse and about exceptions but they dont quite cut what im looking for.
suggestions, tips, and critique are welcome.
thanks in advance!
public class MathTeacher {
public static int addNumbers(int n1, int n2){
int add = n1+n2;
return add;
}
public static int subtractNumbers(int n1, int n2){
int subs = n1-n2;
return subs;
}
public static int multiplyNumbers(int n1, int n2){
int mulp = n1*n2;
return mulp;
}
public static int divideNumbers(int n1, int n2){
int div = n1/n2;
return div;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
int choices;
System.out.println(
"Welcome to *Mental Math Practice* where you can test your addition, subtraction, multiplication, and division.");
System.out.println("Enter 1 to add the two numbers.");
System.out.println("Enter 2 to subtract the second number from the first number.");
System.out.println("Enter 3 to multiply the two numbers.");
System.out.println("Enter 4 to divide the first number by the second number.");
choices = scanner.nextInt();
switch(choices){
case 1: {
System.out.println("Enter two integers: ");
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int add = addNumbers(n1,n2);
System.out.println(add);
continue;
}
case 2: {
System.out.println("Enter two integers: ");
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int sub = subtractNumbers(n1,n2);
System.out.println(sub);
continue;
}
case 3: {
System.out.println("Enter two integers: ");
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int mulp = multiplyNumbers(n1,n2);
System.out.println(mulp);
continue;
}
case 4: {
System.out.println("Enter two integers: ");
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int div = divideNumbers(n1,n2);
System.out.println(div);
continue;
}
}
System.out.println("Enter 'Quit' to end the program.");
}
}
}