Reputation: 53
I have a simple calculator here with a switch statement that should allow the user to enter a string like so: *5
the operator can be - + / or * The code performs correctly during addition, however, when I subtract a value it adds it, and dividing or multiplying will result in the exceptions listed below.
Enter an operator and a number:
+5
Enter an operator and a number:
*2
Exception in thread "main" java.lang.NumberFormatException: For input string: "*2"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at Calculator.whatOperator(Calculator.java:38)
at Calculator.aResult(Calculator.java:29)
at Main.main(Main.java:30)
I believe I am not parsing the double correctly for some reason but am unsure how to do so...I was thinking stringtokenizer but how can I use the tokenizer without a delimiter? Here is my calculator class and below that is my main class
import java.util.Scanner;
public class Calculator {
private final int RESET = 0;
private double number = 0;
private double result = 0;
private char operator;
private Scanner keyboard = new Scanner(System.in);
public Calculator(double number)
{
this.number = number;
}
public void reset()
{
this.number = RESET;
}
public double aResult(Calculator other)
{
other.whatOperator();
this.result = other.result;
return result;
}
public void whatOperator()
{
String operatorString = enterNumber();
// the error occurs here....is there a better way to do this?
double theNumber = Double.parseDouble(operatorString);
char theOperator = operatorString.charAt(0);
this.operator = theOperator;
operatorString ="";
operatorString += theOperator;
// the switch should perform the operation
switch(operatorString){
case "*":
result = getNumber() * theNumber;
break;
case "/":
result = getNumber() / theNumber;
break;
case "+":
result = getNumber() + theNumber;
break;
case "-":
result = getNumber() - theNumber;
break;
case "R":
result = RESET;
break;
case "P":
System.out.println("Goodbye");
System.exit(0);
}
}
public double add(double secondNumber)
{
result = number + secondNumber;
return result;
}
public double divide(double secondNumber)
{
result = number / secondNumber;
return result;
}
public double multiply(double secondNumber)
{
result = number * secondNumber;
return result;
}
public void subtract(double secondNumber)
{
result = number - secondNumber;
}
public double getNumber()
{
return number;
}
public void setNumber(double number)
{
this.number = number;
}
public String enterNumber()
{
System.out.println("Enter an operator and a number:");
String toString = keyboard.nextLine();
return toString;
}
}
public class Main {
public static void main (String[] args) {
Calculator a = new Calculator(0);
a.setNumber(a.aResult(a));
a.setNumber(a.aResult(a));
String theString = String.valueOf(a.getNumber());
System.out.println(theString);
}
}
Upvotes: 0
Views: 49
Reputation: 36987
This line is wrong:
double theNumber = Double.parseDouble(operatorString);
because it parses the operator as part of the number. Instead, use this:
double theNumber = Double.parseDouble(operatorString.substring(1));
Upvotes: 1