Hello everyone,
I have just recently started learning Java (previous Python programmer) and I am making a text based calculator program.

I have had no problems with programming it so far, and I just need help with one part of perfecting it.

``````package calculator;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
String operator, rego;

System.out.println("Welcome to the Calculator.");
while (loop_calc == 1) {
System.out.println(" ");
System.out.print("Enter first number: ");
num1 = myScanner.nextDouble();
System.out.print("Enter operator: ");
operator = myScanner.next();
System.out.print("Enter second number: ");
num2 = myScanner.nextDouble();

if (operator.equals("+")) {
System.out.println(" ");
System.out.println(num1 + " " + "+" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("-")) {
System.out.println(" ");
System.out.println(num1 + " " + "-" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("/")) {
System.out.println(" ");
System.out.println(num1 + " " + "/" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("*") || (operator.equals("x"))) {
System.out.println(" ");
System.out.println(num1 + " " + "x" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("^")) {
System.out.println(" ");
System.out.println(num1 + " " + "^" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else {
System.out.println(" ");
System.out.println("[INVALED OPERATOR!]");
System.out.println(" ");
}

System.out.print("Another calculation? [y/n]: ");
rego = myScanner.next();
if (rego.equals("y") || rego.equals("yes")) {
loop_calc = 1;
} else if (rego.equals("n") || rego.equals("no")) {
loop_calc = 0;
}
}
}
}``````

I would like to make the program so that it is as "user proof" as possible.

So here is my problem, as you can see from the code if the user types anything other than a number type in 'num1' and 'num2' the program will error and end.

I solved a problem like it with the 'else' for the 'operator' variable, but need a way to solve it for the two 'num' variables.

Any ideas or advice would be much appreciated here.

Thanks for the support,
-WolfShield

## All 9 Replies

You could use simple error checking using try and catch like this:

``````import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
String num1s="",num2s="";
boolean isNum = false; //boolean for while loop
String operator, rego;

System.out.println("Welcome to the Calculator.");
while (loop_calc == 1) {
System.out.println(" ");

while(!isNum)
{
System.out.print("Enter first number: ");
num1s = myScanner.next();

try {
num1 = Double.parseDouble(num1s);
isNum = true;
}
catch(NumberFormatException nFE)
{
System.out.println("Not an Integer");

}
}

isNum=false; //set to default for the second while loop
System.out.print("Enter operator: ");
operator = myScanner.next();
while(!isNum)
{
System.out.print("Enter second number: ");
num2s = myScanner.next();
try {
num2 = Double.parseDouble(num2s);
isNum = true;

}
catch(NumberFormatException nFE)
{
System.out.println("Not an Integer");

}
}

num1 = Double.parseDouble(num1s);
num2 = Double.parseDouble(num2s);

if (operator.equals("+")) {
System.out.println(" ");
System.out.println(num1 + " " + "+" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("-")) {
System.out.println(" ");
System.out.println(num1 + " " + "-" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("/")) {
System.out.println(" ");
System.out.println(num1 + " " + "/" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("*") || (operator.equals("x"))) {
System.out.println(" ");
System.out.println(num1 + " " + "x" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else if (operator.equals("^")) {
System.out.println(" ");
System.out.println(num1 + " " + "^" + " " + num2 + " " + "=" + " " + answer);
System.out.println(" ");

} else {
System.out.println(" ");
System.out.println("[INVALED OPERATOR!]");
System.out.println(" ");
}

System.out.print("Another calculation? [y/n]: ");
rego = myScanner.next();
if (rego.equals("y") || rego.equals("yes")) {
loop_calc = 1;
} else if (rego.equals("n") || rego.equals("no")) {
loop_calc = 0;
}
}
}
}``````

This is one way of doing it.

Sorry, I forgot to add the isNum = false; after the 2nd inner while loop. Also, You don't need to set num1 and num2 in 2 places. Just have Double.parseDouble(num1s) in the try statements, no need to equate it to num1 and num2, you do that at the end once you know they are all numbers

I agree with Akill10.
The easiest way to fix your issue is to use a try-catch.
It's the java way of handling exceptions.
Have a look at the java api on exceptions for the numerous different exceptions that can occur if your interested;

To handle an exception all you have to do is type

``````try{
}
catch (IOException e)
/* This is an example of an exception. The IOException is the most general class of exceptions. Most other exceptions are subclasses of IOException. Otherwise you are handling a RuntimeException which is generally due to bad programming.
*/
{
//What you want to do with your exception here.
}``````

If however your making a method and you don't want to handle the exception in your method you can put "throws" followed by our exception in the title for example:

``````public add(String s) throws NumberFormatException
{
Integer.parseInt(s);
}``````

This way you can use multiple methods in your main and not have to make a try-catch for each one.

PS: I forgot to mention (and so did Akill), you have to import another package to handle exceptions:

``import java.io.*;//The astericks means all the subclasses of java.io are imported.``

PS: I forgot to mention (and so did Akill), you have to import another package to handle exceptions:

``import java.io.*;//The astericks means all the subclasses of java.io are imported.``

All classes in the java.lang package are imported by default.

For the exception in my example, I didn't use an IO exception. I used a NumberFormatException which is part of the java.lang package, so it doesn't need an import.

Ah yes, sorry about that I didn't know it didn't have to be imported...

Thanks guys!

Now my program is a user proof one! You guys are the best.

One question (and possibly more), what does the

``num1 = Double.parseDouble(num1s);``

do exactly?

Thanks,
-WolfShield

Thanks guys!

Now my program is a user proof one! You guys are the best.

One question (and possibly more), what does the

``num1 = Double.parseDouble(num1s);``

do exactly?

Thanks,
-WolfShield

As you can see, we asked the user for the number input using the String num1s,num2s.
parseDouble() is a method of the Double class that attempts to convert a String into a double. The try...catch block tests whether it can be "converted" by handling an exception. Therefore, once the try part succeeds, we know that the String num1s,num2s can be converted into a double. We can then set num1 to equal the double that was converted.

There are similar methods in the other 'Number'classes. i.e. Integer.parseInt();

Thanks!
Is there one for doing the same thing but find out if it is anything other than a string?

(Obviously I am needing a LOT of teaching :))

-WolfShield

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.