Is there a basic solution I am missing here? Side note, I don't particularly want a super advanced solution as I am in the first 8 weeks of a computer science course in college and I feel it is either something simple I am missing or something I haven't learned yet.
Please focus on what I am asking, I know the code can be cleaner for now I don't want tips on how to make it more efficient I know there is too much repetition.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
/*Initializing a string which will be
*used in the future to determine the
*operator the user wants to use*/
String operator = "z"; //Cant be A,S,M or D or the program would skip my while loop
//Creating number variables which will initilised later by user input//
int numOne;
int numTwo;
//Creating a boolean variable asking the user if they would like
//to run another calculation and a String to store Yes or No
boolean anotherCalc = true;
String runAgain = "p";
Scanner userOperator = new Scanner(System.in);
Scanner userNumbers = new Scanner(System.in);
Scanner userAgain = new Scanner(System.in);
while (anotherCalc == true) { //Containing the whole calculator in this while loop
//so it only stops when the user says they don't want another calc
while (!operator.equalsIgnoreCase("A") && !operator.equalsIgnoreCase("S")
&& !operator.equalsIgnoreCase("M") && !operator.equalsIgnoreCase("D")
&& operator.equalsIgnoreCase("z")){
System.out.println("What kind of calculation would you like to make?");
System.out.println("Press A for Addition\nPress S for Substraction");
System.out.println("Press M for Multiplication\nPress D for Division");
operator = userOperator.next();
}
System.out.println("\nThank you, now what numbers would you like to use?");
System.out.println("Enter number 1:");
String numError;
numError = userNumbers.next();
numOne = Integer.parseInt(numError);
System.out.println("Enter number 2:");
String numError2;
numError2 = userNumbers.next();
numTwo = Integer.parseInt(numError2);
int answer;
//Starting outer if statement containing nested if statements for
//all 4 operators
//Addition if statement with nested if statement that won't
//break the code if someone trys to add outside the int value range
if (operator.equalsIgnoreCase("A")){
if (numOne > 1073741824 || numTwo > 1073741824){
long answerlong = (long) numOne + (long) numTwo;
System.out.print("\nYour answer is " + answerlong);
} else {answer = numOne + numTwo;
System.out.println("\nYour answer is " + answer);}
//Subtraction if statement with nested if statement that won't
//break the code if someone trys to subtract from a number above the int value range
} else if (operator.equalsIgnoreCase("S")){
if (numOne > 2147483647 || numTwo > 2147483647){
long answerlong = (long) numOne - (long) numTwo;
System.out.print("\nYour answer is " + answerlong);
} else {
answer = numOne - numTwo;
System.out.println("\nYour answer is " + answer);
}
} else if (operator.equalsIgnoreCase("M")){
if (numOne > 20000 || numTwo > 20000){
long answerlong = (long) numOne * (long) numTwo;
System.out.print("\nYour answer is " + answerlong);
} else {
answer = numOne * numTwo;
System.out.println("\nYour answer is " + answer);
}
} else if (operator.equalsIgnoreCase("D")){
if (numOne % numTwo == 0){ //Nested if else statement so
answer = numOne / numTwo; //an answer with no decimal
System.out.println("\nYour answer is " + answer); //point is shown as a whole
//number and otherwise return decimals
} else {
float answerdec = (float) numOne / (float) numTwo;
System.out.printf("\nYour answer is " + "%.2f",answerdec);}
} else {
System.out.println("\nError");
}
System.out.println("\nWould you like to run another calculation?");
System.out.println("Press Y for Yes or N for No");
runAgain = userAgain.next();
if (runAgain.equalsIgnoreCase("Y")){
anotherCalc = true;
operator = "z"; //This needs to be added so it returns to ask the user what type of calculation
System.out.println("\n\n"); //This is just for aesthetics, creating 2 lines before the program runs again. Not included on the first run of the program on purpose.
} else if (runAgain.equalsIgnoreCase("N")){
anotherCalc = false;
} else {
anotherCalc = false;
System.out.println("\n\nInput Error (Y or N not entered)\nPlease reload the program");
}
}
System.out.println("\n\nThank you for using the calculator");
System.out.println("I hope you enjoyed it");