Reputation: 3
.class expected on line 68 return double result; The program wont compile but I cant figure it out can anyone help?
It is a basic calculator java file tkaes the user numbers and performs math on them depending on the operator entered
// Calculator.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input: Interactive.
// Output: Result of arithmetic operation
import javax.swing.*;
public class Calculator
{
public static void main(String args[])
{
double numberOne, numberTwo;
String numberOneString, numberTwoString;
String operation;
double result ;
numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
numberOne = Double.parseDouble(numberOneString);
numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
numberTwo = Double.parseDouble(numberTwoString);
operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
// Call performOperation method here
result = performoperation(numberOne, numberTwo, operation);
System.out.format("%.2f",numberOne);
System.out.print(" " + operation + " ");
System.out.format("%.2f", numberTwo);
System.out.print(" = ");
System.out.format("%.2f", result);
System.exit(0);
} // End of main() method.
// Write performOperation method here.
public static double performOperation(double numberOne, double numberTwo, String operation)
{
double result = 0;
if (operation .equals("+")){
result = numberOne + numberTwo;
}
else if (operation .equals("-")){
result = numberOne - numberTwo;
}
else if (operation .equals("/")){
result = numberOne / numberTwo;
}
else if (operation .equals("*")){
result = numberOne * numberTwo;
}
else if (operation .equals("%")){
result = numberOne % numberTwo;
}
return double result;
System.out.println("The result is " + result);
}// END
} // End of Calculator class.
Upvotes: 0
Views: 120
Reputation: 71
You have multiple errors:
1.Error: This is incorrect, because the method's name is performOperation:
result = performoperation(numberOne, numberTwo, operation);
So you must change it to:
result = performOperation(numberOne, numberTwo, operation);
2. Error: The syntax is incorrect here and if you think about it, having code after the return statement is pointless, since the method will return with whatever value it has:
return double result;
System.out.println("The result is " + result);
So change this to:
System.out.println("The result is " + result);
return result;
Two things which are worth mentioning:
when you compare variables with a String for example, it is preferred to do it like this:
"+".equals(operation)
This way you can avoid a NullPointerException (think about what could happen if your code gets a null as the operation for some reason? it would compare null with the "+" String and you would get the NullPointerException). You can take a look into the concept of Enum to handle the operations in a safer way ;)
Upvotes: 0
Reputation:
You have some mistakes in your code:
You're trying to return double result
, when in fact, you should only return result
.
In your main method, you're calling for a method that doesn't exist, namely 'performoperation', while yours is called 'performOperation'. Java is case sensitive.
You're trying to print the result after you've returned it, and this won't work. You have to print before you return.
I fixed up some of your code, and also converted your if/else statements to a switch case (easier to deal with when you have repetitive code in my opinion).
import javax.swing.*;
public class Calculator {
public static void main(String args[]) {
String numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
double numberOne = Double.parseDouble(numberOneString);
String numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
double numberTwo = Double.parseDouble(numberTwoString);
String operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
// Call performOperation method here
double result = performOperation(numberOne, numberTwo, operation);
System.out.format("%.2f", numberOne);
System.out.print(" " + operation + " ");
System.out.format("%.2f", numberTwo);
System.out.print(" = ");
System.out.format("%.2f", result);
System.exit(0);
} // End of main() method.
// Write performOperation method here.
public static double performOperation(double numberOne, double numberTwo, String operation) {
double result = 0;
switch (operation) {
case "+":
result = numberOne + numberTwo;
break;
case "-":
result = numberOne - numberTwo;
break;
case "/":
result = numberOne / numberTwo;
break;
case "*":
result = numberOne * numberTwo;
break;
case "%":
result = numberOne % numberTwo;
break;
default:
break;
}
System.out.println("The result is " + result);
return result;
}// END
} // End of Calculator class.
Upvotes: 3