Question:
Why won't this java code compile?
Rafael
2010-10-27 18:18:10 UTC
// ***************************************************************
// Salary.java
//
// Computes the amount of a raise and the new
// salary for an employee. The current salary
// and a performance rating (a String: "Excellent",
// "Good" or "Poor") are input.
// ***************************************************************
import java.util.Scanner;
import java.text.NumberFormat;
public class Salary {
public static void main (String[] args){

double currentSalary; // employee's current salary
double raise; // amount of the raise
double newSalary; // new salary for the employee

String rating; // performance rating
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the current salary: ");
currentSalary = scan.nextDouble();
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
rating = scan.nextLine();

// Compute the raise using if ...

if (rating == "Excellent")
{raise = currentSalary * 0.06;
newSalary = currentSalary + raise;
}
if (rating == "Good")
{raise = currentSalary * 0.04;
newSalary = currentSalary + raise;
}
if (rating == "Poor")
{raise = currentSalary * 0.015;
newSalary = currentSalary + raise;
}

// Print the results
NumberFormat money = NumberFormat.getCurrencyInstance();
System.out.println();
System.out.println("Current Salary: " + money.format(currentSalary));
System.out.println("Amount of your raise: " + money.format(raise));
System.out.println("Your new salary: " + money.format(newSalary));
System.out.println();

}
}

The problem is that JCreator says the variables 'raise' and 'newSalary' aren't initialized in the printing process. What's wrong with the code?
Four answers:
Ratchetr
2010-10-27 18:27:10 UTC
Java does a path analysis on your code to determine if there is some path such that the variable raised or newSalary is used before it is assigned a value. If it finds one, that is considered an error.



Ok, it found one (or you wouldn't get the error message).

What is it?



Suppose the user enters SUCKS! when you prompt for performance rating. Will your code assign a value to raise or newSalary before you get to the System.out.println's that use those variables?



That's what Java is upset about. You've coded the happy path (user always does exactly what you tell them to do and they never make typing mistakes). But people don't always do what you tell them to do, they do make typing mistakes, and Java knows that.



You need to cover the not so happy path, then the compiler will be happy.
green meklar
2010-10-27 18:47:31 UTC
Okay, you have to understand what variable initialization is. There are two steps in making a variable available for use, declaration and initialization. For instance, here I'm declaring an integer:



int a;



Here I'm subsequently initializing that same integer, giving it the value 1:



a=1;



I can also do both in the same line, like this:



int a=1;



The same thing applies to objects, for instance let's say I'm using Java's native Integer class which is a wrapper for an int. In that case:



Integer b; //declaration

b=new Integer(); //initialization

Integer b=new Integer(); //declaration and initialization



Now, it turns out your code will automatically produce a compile error if you try to use a variable without having first initialized it. Whether you declare it or not doesn't matter, if it's not initialized when you try to use it then it won't work. But the compiler preemptively looks at your code to figure out whether the variable will be initialized, and it DOES NOT COUNT initialization which occurs inside conditional blocks (unless ALL the possible variations on the conditional block contain an initialization). For instance, this will not compile:



Integer b;

if(some boolean statement)

{

b=new Integer();

}

System.out.print(b.intValue());



This, however, WILL compile:



Integer b;

if(some boolean statement)

{

b=new Integer();

}

else

{

b=new Integer();

}

System.out.print(b.intValue());



because both the if block and the else block initialize b, so it has to be initialized before being used either way. This will also compile:



Integer b=null;

if(some boolean statement)

{

b=new Integer();

}

System.out.print(b.intValue());



because I initialized b to null before using it. In this latter case, if the if statement is NOT run, then when the program hits b.intValue(), it will throw an exception. However, it still compiles properly.



In your case, you have not initialized raise or newSalary outside of if statements. As a result, the compiler thinks it is possible for them not to be initialized, and produces the compile error. Since raise and newSalary are just doubles, I would suggest that to fix this problem, you simply change this part of your code:



double raise; // amount of the raise

double newSalary; // new salary for the employee



to this:



double raise=0; // amount of the raise

double newSalary=0; // new salary for the employee



Voila, now it compiles properly. Or at least, it doesn't produce the same compile error you got earlier.
Otherworld
2010-10-27 18:29:24 UTC
The problem you have is not big.

When you declare a variable, you also need to initialize it if you want to use it in other methods. The simplest way is to:

double raise=0; // amount of the raise

double newSalary=0; // new salary for the employee



I did not look at the rest of the code, so maybe you have other mistakes
balakrishnan
2010-10-27 20:02:38 UTC
initialize your variables , the error pops up only when your code goes to else part.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...