0

So the compiler is saying my inches variable in the second print line statement needs to be initialized. Why is that? Why can't I leave the variable as an unknown? When I don't have the if/else statement, then it works fine.

import java.util.Scanner;

class FeetToInchesInputOutput {
public static void main(String[] args){

 double feet;
 double inches;
 String userFeetInput;

 Scanner input = new Scanner(System.in);

 System.out.print("How many feet do you want to convert?");
 userFeetInput = input.nextLine();
 feet = Double.parseDouble(userFeetInput);

 if (feet < 500.0) {
 inches = feet*12;
 }
else {
system.out.println("Type a number less than 500.");
}

System.out.println(feet + " feet is equal to " + inches + " inches.");
System.exit(0);
}
}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Jeremy
  • 59
  • 6

5 Answers5

1

In Java local variables are not initialized automatically. you have to initialize it before using it in code. In your code if condition is true it will get initialized but compiler doesn't know whether condition is true or false. It is decided at runtime. And in else condition variable inches is not being initialized(or you say being assigned a value). So it's throwing an compilation error. You can even initialize object with null.

You have to initialize it because the creator of Java decided that the local variable won't get default value like class level variables get. So you have to initialize it.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
0

Java does not allow using (in your case - printing) unitialized variables. Here, if feet is larger than 500, inches is never initialized, so it can't be used. One way to solve this is to move the printing into the if statement:

if (feet < 500.0) {
    inches = feet*12;
    System.out.println(feet + " feet is equal to " + inches + " inches.");
} else {
    system.out.println("Type a number less than 500.");
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

The reason you need to initialize is because, if that if statement goes into the else, inches is not initialized. If you had also assigned inches in the else, then there would not be a problem, as not matter what, inches would have a value when the program executes the System.out.println(feet + " feet is equal to " + inches + " inches."); line. The better way (IMHO) is to assign a value when you first create the variable, I use -1 so that I know that the value is not right.

Gliderman
  • 1,195
  • 9
  • 18
0

Look at the flow of your program: it is possible that the line where you use the variable is reached without having the variable being initialized, and that is forbidden.

You observation is correct: if feet >= 500.0, your inches is never initialized.

There is a number of solutions:

  1. Only print the result if you have one:

    if (feet < 500.0) {
        inches = feet*12;
        System.out.println(feet + " feet is equal to " + inches + " inches.");
    } else {
        system.out.println("Type a number less than 500.");
    }
    
  2. Loop until you get a valid result:

    do {
        System.out.print("How many feet do you want to convert?");
        userFeetInput = input.nextLine();
        feet = Double.parseDouble(userFeetInput);
    
        if (feet >= 500.0) {
            system.out.println("Type a number less than 500.");
        }
    } while (feet >= 500.0);
    inches = feet*12;
    ...
    

In both cases your inches won't be used without having been initialized, and that will satisfy the compiler.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

For the if block to be able to proceed at execution of the script, it needs to be able to reach a conclusion. You have written

if (var < IntValue) {
   do some stuff 
}

and you haven't given var a value. How does the evaluation complete? You didn't add another test to check to see if var is null which is what you have presently for var. Add that check if you don't want to assign a value to var and you want the if block.

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51