I'm making a calculator that asks for inputs in the console and prints answers, nothing fancy. I've got it all working, even detecting if you have inputed an integer lower than 0 when square rooting.

At the moment I have it so you type in a string which asks you to type in integers. The problem I'm having is detecting if the string is wrong rather than the stringandthe integer.

Here's my code:

Code :

import java.util.Scanner; import static java.lang.System.*; public class CalculatorConsole { @SuppressWarnings({ "resource" }) public static void main(String args[]) { out.println("Please select an operation from the list:"); out.println("- sqrt"); out.println("- add"); out.println("- subtract"); out.println("- times"); out.println("- divide"); out.println("- terminate"); String sqrt = "sqrt"; String add = "add"; String subtract = "subtract"; String times = "times"; String divide = "divide"; String terminate = "terminate"; int loop = 0; while (loop != 1) { Scanner input = new Scanner(in); String output = input.next(); if (output.equals(sqrt)) { out.print("Please enter a number to square root: "); int sqrtInput = input.nextInt(); if (sqrtInput < 0) { out.println("You are a silly person, you cannot use anything below zero"); } else { out.printf("The square root of " + sqrtInput + " is %1.1f\n", Math.sqrt(sqrtInput)); } } if (output.equals(add)) { out.println("Please enter two numbers to add: "); int addInput1 = input.nextInt(); int addInput2 = input.nextInt(); out.println((addInput1) + " + " + (addInput2) + " = " + (addInput1 + addInput2)); } if (output.equals(subtract)) { out.println("Please enter two numbers to subtract: "); int subtractInput1 = input.nextInt(); int subtractInput2 = input.nextInt(); out.println((subtractInput1) + " - " + (subtractInput2) + " = " + (subtractInput1 - subtractInput2)); } if (output.equals(times)) { out.println("Please enter two numbers to times: "); int timesInput1 = input.nextInt(); int timesInput2 = input.nextInt(); out.println((timesInput1) + " * " + (timesInput2) + " = " + (timesInput1 * timesInput2)); } if (output.equals(divide)) { out.println("Please enter two numbers to divide: "); int divideInput1 = input.nextInt(); int divideInput2 = input.nextInt(); if (divideInput2 == 0) { out.println("You are a silly person, you cannot use 0"); } else { out.println((divideInput1) + " / " + (divideInput2) + " = " + (divideInput1 / divideInput2)); } } if (output.equals(terminate)) { out.println(); out.println("##################"); out.println("PROGRAM TERMINATED"); out.println("##################"); loop++; } if (!output.equals (sqrt + times + add + divide + subtract)) { out.println("Incorrect input"); } if (loop != 1) { out.println(); out.println("Please choose another operation"); } } } }

Thanks in advance!