-1
    package fr.xeira.programme;

import java.util.Scanner; // import the Scanner class 

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    String name;
    System.out.println("Enter First Number");

    String firstnumber = myObj.nextLine();
    System.out.println("Enter Symbol");
    String symbol = myObj.nextLine();
    System.out.println("Enter Second Number");
    String secondnumber = myObj.nextLine();

    int number = Integer.parseInt(firstnumber);

    int number2 = Integer.parseInt(secondnumber);

    int final1; //africa wakaka wakakakakakakakkakakakakakakwaaaa AFRIICAAAAAA WAWAKKAKAKWAKKAKKAK HEHEHEHE
    if (symbol == "+") {
        final1 = (number + number2);
    } else if(symbol == "-") {
        System.out.println(number - number2);
    } else if(symbol == "*") {
        System.out.println(number * number2);
    } else if(symbol == "/") {
        System.out.println(number / number2);
    }

    System.out.println(final1);
    }

}

It tells me (line 32) that "the local variable has not been initialized". Why is that? I initialize before the if statement, change it during the if statement, and now I'm simply trying to call it.

I'd apreciate some help ^^

xef5000
  • 5
  • 2
  • You do not initialize it before the if statement. You *only* initialize it in the if statement, so if `symbol` is anything other than `"+"` (also [don't use `==` for strings](https://stackoverflow.com/questions/767372/string-equals-versus)) then it's uninitialized. – Silvio Mayolo Apr 03 '22 at 02:42
  • Because it's possible that the user might type in something other than +, -, * or / and then `symbol` will never have been assigned a value.j Additionally, you should be using `symbol.equals("+")`, etc. A double equals there is asking is the two strings occupy the same space in memory, which is not what you want. You're want to know if the contents of the two strings are equivalent. – Idle_Mind Apr 03 '22 at 02:45

1 Answers1

0

When you declare int final1 on line 21, you have declared the variable, but not yet initialized because it has no value. You conditionally initialize it on line 23, but the compiler is detecting that the variable could not have a value by the time it reaches line 32 in runtime, so you get a compile error.

To fix, set final1 to a value for every possible condition, e.g.

int final1; //africa wakaka wakakakakakakakkakakakakakakwaaaa AFRIICAAAAAA WAWAKKAKAKWAKKAKKAK HEHEHEHE
if (symbol == "+") {
    final1 = (number + number2);
} else if(symbol == "-") {
    final1 = number - number2);
} else if(symbol == "*") {
    final1 = number * number2);
} else if(symbol == "/") {
    final1 = number / number2);
} else {
    final1 == //todo some value;
}

System.out.println(final1);
MarkC
  • 26
  • Should be `symbol.equals("+")`, etc... – Idle_Mind Apr 03 '22 at 02:50
  • It doesn't work.. Before: https://prnt.sc/7wfQI1qa5AF0 After: https://prnt.sc/NRGN7WoivrLG – xef5000 Apr 03 '22 at 02:57
  • 1
    That's because you're redeclaring 'int final1' in each scope (except, inexplicably, one case) . You need to understand the difference between 'declaration' and 'assignment'. Or you compare hat you wrote to the answer. – passer-by Apr 03 '22 at 03:00
  • Oh my god it worked! I looked it up and searched in documentations and found my answer. Thanks – xef5000 Apr 03 '22 at 03:04