I am looking for the answer to how do I make my program run correctly. I want my program to recognize things other than doubles--- for example, if i placed in idk it would say, that is not a number instead of crashing.
my code:
import java.util.*;
public class CalculatorMain {
public static void main(String[] args) {
FunctionMain action = new FunctionMain();
Scanner input = new Scanner(System.in);
double answer = 0;
double inputNu1, inputNu2;
char operator;
boolean end = false;
while (! end ) {
System.out.print(">>>");
inputNu1 = input.nextDouble();
operator = input.next().charAt(0);
inputNu2 = input.nextDouble();
switch (operator)
{
case '-': answer = action.Subtract(inputNu1, inputNu2);
break;
case '*': answer = action.Multiply(inputNu1, inputNu2);
break;
case '+': answer = action.Add(inputNu1, inputNu2);
break;
case '/': answer = action.Divide(inputNu1, inputNu2);
break;
}
System.out.println(answer);
}
input.close();
}
}
public class FunctionMain {
double Subtract (double Nu1 , double Nu2)
{
return Nu1 + Nu2;
}
double Multiply (double Nu1 , double Nu2)
{
return Nu1 * Nu2;
}
double Add (double Nu1 , double Nu2)
{
return Nu1 + Nu2;
}
double Divide (double Nu1 , double Nu2)
{
return Nu1 / Nu2;
}
}