Reputation: 45
following code is giving error message as 'Exception in thread "main" java.lang.Error: Unresolved compilation problem: The local variable answer may not have been initialized'.
package firstproject;
import java.util.Scanner;
public class calculator {
public static void main(String args[]){
double firstno, secondno, answer;
int choice;
Scanner add = new Scanner(System.in);
System.out.println("What r u trying to do??");
System.out.println("1 - add");
System.out.println("2 - substract");
System.out.println("3 - multiplication");
System.out.println("4 - division");
choice = add.nextInt();
System.out.print("Enter your 1st number: ");
firstno = add.nextDouble();
System.out.print("Enter another number: ");
secondno = add.nextDouble();
if(choice == 1){
answer = firstno + secondno;
}
if(choice == 2){
answer = firstno - secondno;
}
if(choice == 3){
answer = firstno * secondno;
}
if(choice == 4){
answer = firstno / secondno;
}
System.out.println("answer: " + answer);
}
}
so, i did something like this
package firstproject;
import java.util.Scanner;
public class calculator {
public static void main(String args[]){
double firstno, secondno, answer;
int choice;
Scanner add = new Scanner(System.in);
System.out.println("What r u trying to do??");
System.out.println("1 - add");
System.out.println("2 - substract");
System.out.println("3 - multiplication");
System.out.println("4 - division");
choice = add.nextInt();
System.out.print("Enter your 1st number: ");
firstno = add.nextDouble();
System.out.print("Enter another number: ");
secondno = add.nextDouble();
if(choice == 1){
answer = firstno + secondno;
System.out.println("Answer: " + answer);
}
if(choice == 2){
answer = firstno - secondno;
System.out.println("Answer: " + answer);
}
if(choice == 3){
answer = firstno * secondno;
System.out.println("Answer: " + answer);
}
if(choice == 4){
answer = firstno / secondno;
System.out.println("Answer: " + answer);
}
}
}
it worked. but i want to know why my first code didn't work.
Upvotes: 1
Views: 3958
Reputation: 9
The var "answer" may not be inited if "choice" does not belong to "1,2,3,4". So when you try to print it at the very end, this exception may happen.
The second one actually has the same problem, the only reason that makes it work is you moved the print line up to each blocks. So when "choice" does not belong to "1,2,3,4", the code needs to do nothing. And it will be considered as "No problem".
For the fix, all of them above :)
Upvotes: 0
Reputation: 1292
"The local variable answer may not have been initialized" is accurate. If the user types in "5", then answer doesn't get set to any value. Try defining answer as "answer = 0.0".
Upvotes: 0
Reputation: 504
Initialize the answer
with some values like answer=-1
before printing, because how we can print a null reference
once any variable initialized it can be use in body of conditional statements
suppose You don't enter valid choice , what would be the value in answer
, while answer
is just act like a null reference
Upvotes: 0
Reputation: 1502006
Local variables in Java can't be read unless they're definitely assigned - in other words, if the compiler can prove that every way that you can get to the expression that's trying to read the variable will have gone through an assignment to that variable.
In your case, if choice
isn't 1, 2, 3 or 4 you won't have gone through an assignment.
Ways of fixing this:
Change your if
series to an if/else
series with a plain else
at the end, which either terminates the method or assigns a value. For example:
if (choice == 1) {
answer = firstno + secondno;
} else if (choice == 2) {
answer = firstno - secondno;
} else if (choice == 3) {
answer = firstno * secondno;
} else if(choice == 4) {
answer = firstno / secondno;
} else {
System.out.println("Invalid choice");
return; // Or assign a value to answer
}
switch
statement instead of your if
statements, and provide a default
case which either assigns to answer
or returnsUpvotes: 3
Reputation: 1523
You can access it, but if none of your conditions was true
, answer
will not be initialized when you are trying to print it.
Initialize it with double answer = 0
.
Upvotes: 5
Reputation: 5255
if all the conditions in all ifs are false, the variable answer will indeed not be initialized.
Say choice is 42245234, what value does answer have in this line?
System.out.println("answer: " + answer);
Upvotes: 0