why the value inside 'if statement' in java cannot be accessed outside of 'if statement'?

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.

Jon Skeet
people
quotationmark

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:

  • Specify an initial value as part of the variable declaration
  • 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
    }
    
  • Use a switch statement instead of your if statements, and provide a default case which either assigns to answer or returns

people

See more on this question at Stackoverflow