Reputation: 9
I'm sorry if my title isn't specific enough. I'm trying to create a calculator. You enter: "operation" "num1" "num2"
and you get your answer.
Currently, I receive the following error from the code below:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at LessonOne.Main.calcu(Main.java:17)
at LessonOne.Main.main(Main.java:13)
My inputs are, "+", "5", "5"
Please tell me what I'm doing wrong. I spent a couple hours editing this and this is the best I've come up with. I'm totally stuck.
package LessonOne;
import java.util.Scanner;
public class Main {
public static Scanner s = new Scanner(System.in); //<-- DO NOT TOUCH
public static void main(String[] args) {
String oper = getStr(); //Enter your operator
int numberOne = getInteger(); //Enter your first number
int numberTwo = getInteger(); //Enter your second number
int total = calcu(oper, numberOne, numberTwo); //Perform calculation
System.out.println(total); //Print the total
}
static int calcu(String oper, int numberOne, int numberTwo) {
if (oper.equals('+')) {
int total = add(numberOne, numberTwo);
return total;
} else if (oper.equals('-')) {
int total = sub(numberOne, numberTwo);
return total;
} else if (oper.equals('*')) {
int total = multi(numberOne, numberTwo);
return total;
} else if (oper.equals('/')) {
int total = div(numberOne, numberTwo);
return total;
}
}
static int add(int numberOne, int numberTwo) {
return int total = numberOne + numberTwo;
}
static int multi(int numberOne, int numberTwo) {
return int total = numberOne * numberTwo;
}
static int sub(int numberOne, int numberTwo) {
return int total = numberOne - numberTwo;
}
static int div(int numberOne, int numberTwo) {
return int total = numberOne / numberTwo;
}
public static String getStr() {//<-- DO NOT TOUCH
return s.nextLine();//<-- DO NOT TOUCH
}//<-- DO NOT TOUCH
public static int getInteger() { //<-- DO NOT TOUCH
return s.nextInt(); //<-- DO NOT TOUCH
} //<-- DO NOT TOUCH
}
Upvotes: 0
Views: 2670
Reputation:
Make Sure that Every function with return type returns any value...Your program fails in two conditions ,First is with Calc Function(No value is returned if all if conditions Fails...)
,Second is add(),sub(),mult() and div() cannot have statement like "return int total" ;
public class Main { public static Scanner s = new Scanner(System.in); //<-- DO NOT TOUCH
public static void main(String[] args) {
String oper = s.next();
int numberOne = s.nextInt(); //Enter your first number
int numberTwo = s.nextInt(); //Enter your second number
int total = calcu(oper, numberOne, numberTwo); //Perform calculation
System.out.println(total); //Print the total
}
static int calcu(String oper, int numberOne, int numberTwo) {
if (oper.equals("+")) {
int total = add(numberOne, numberTwo);
return total;
} else if (oper.equals("-")) {
int total = sub(numberOne, numberTwo);
return total;
} else if (oper.equals("*")) {
int total = multi(numberOne, numberTwo);
return total;
} else if (oper.equals("/")) {
int total = div(numberOne, numberTwo);
return total;
}
System.out.println("Invalid Operator...");
return -1;
}
static int add(int numberOne, int numberTwo) {
return numberOne + numberTwo;
}
static int multi(int numberOne, int numberTwo) {
return numberOne * numberTwo;
}
static int sub(int numberOne, int numberTwo) {
return numberOne - numberTwo;
}
static int div(int numberOne, int numberTwo) {
return numberOne / numberTwo;
}
}
Code in Ideone : http://ideone.com/Jp8P4M
My small suggestion is to use Float as Data type for calculator.(it looks cool and pretty)
Upvotes: 1
Reputation: 425208
The problem is that your method calcu()
doesn't return anything if the operand is not one of the ones you're testing for.
Add either a return statement after the last test, or throw an exception. I recommend throwing an unchecked exception:
throw new IllegalArgumentException("Unknown operation " + oper);
Upvotes: 1
Reputation: 31603
Your method 'calcu' must return an integer after the if-else block. You only return an int if one of the blocks is true. But you must also return one by default:
static int calcu(String oper, int numberOne, int numberTwo) {
if (oper.equals('+')) {
int total = add(numberOne, numberTwo);
return total;
} else if (oper.equals('-')) {
int total = sub(numberOne, numberTwo);
return total;
} else if (oper.equals('*')) {
int total = multi(numberOne, numberTwo);
return total;
} else if (oper.equals('/')) {
int total = div(numberOne, numberTwo);
return total;
}
// that one is missing
return -1;
}
Upvotes: 0