Basically I'm trying to make a java calculator. I think its taking the output as input but I'm not sure. It keep taking some sort of input when I didn't input anything. Anyone have any idea why? The repo can be found at https://github.com/Croticalism/Java-calculators if anyone wants to take a look at the whole thing. I really have no idea why this happening in some places but it doesn't in other.
static void SC() {
// stores two numbers
double num1 = 0.0;
double num2 = 0.0;
boolean valid = false;
char op = 0;
while (!valid) {
try {
System.out.println("Enter the first number");
num1 = sc.nextInt();
valid = true;
// catches the mismatch exception so it will only take int input. If letter
// return Not a number
} catch (InputMismatchException e) {
System.out.println("Not a number.");
sc.next();
}
}
while (!valid) {
try {
System.out.println("Enter the second number");
num2 = sc.nextInt();
valid = true;
// catches the mismatch exception so it will only take int input. If letter
// return Not a number
} catch (InputMismatchException e) {
System.out.println("Not a number.");
sc.next();
}
}
while (!valid) {
try {
System.out.println("Enter the operator (+,-,*,/)");
op = sc.next().charAt(0);
valid = true;
// catches the mismatch exception so it will only take int input. If letter
// return Not a number
} catch (InputMismatchException e) {
System.out.println("Not a number.");
sc.next();
}
}
double o = 0;
switch (op) {
// case to add two numbers
case '+':
o = num1 + num2;
break;
// case to subtract two numbers
case '-':
o = num1 - num2;
break;
// case to multiply two numbers
case '*':
o = num1 * num2;
break;
// case to divide two numbers
case '/':
o = num1 / num2;
break;
default:
System.out.println("You enter wrong input");
break;
}
System.out.println("The answer is:");
System.out.println();
// print the final result
System.out.println(num1 + " " + op + " " + num2
+ " = " + o);
}
I have a lot of methods, this is one of the methods that are causing problems ^
System output:
Enter the first number 5 You enter wrong input The answer is:
5.0 0.0 = 0.0
The choice you entered is invalid. Pick one from below
Here is the terminal output that I get ^