"Variable might not have been initialized" error
Hey guys, I am new around here and I am currently enrolled in a Java class at my University and I am having trouble removing an annoying error in my homework here. I am getting the "variable might not have been initialized" error with regards to my variable 'distance'. This program is due by 8 o'clock tonight and I was really hoping this forum could help me!! Thanks beforehand and I expect to be around here more often because I enjoy coding but I frequently get snagged and I'll need help!
// Adam Verheyen
// February 16, 2007
// Chapter 4 Exercise 9
//
// This program will design a class with several methods and will calculate the speed of sound
// through air, water, and steel.
import java.util.Scanner;// Needed for Scanner Class
import javax.swing.JOptionPane;// Needed for onscreen prompts
publicclass VerheyenCh4Ex9
{
publicstaticvoid main(String[] args)
{
String inputMedium, inputString;
short medium;
double distance;
// Create Scanner object to read input
Scanner keyboard =new Scanner(System.in);
// Create speed object
Speed speedObject =new Speed(distance);
// Prompt user to enter "air", "water", or "steel"
inputMedium =
JOptionPane.showInputDialog("Which medium are you calculating? \n (Please Enter A for 'Air', W for 'Water', or S for 'Steel')");
medium = Short.parseShort(inputMedium);
inputString =
JOptionPane.showInputDialog("What is the distance (in feet) the speed is traveling?");
distance = Double.parseDouble(inputString);
if (medium =='A')
{
JOptionPane.showMessageDialog(null,"The speed of sound in air = " + speedObject.getSpeedInAir());
}
if (medium =='W')
{
JOptionPane.showMessageDialog(null,"The speed of sound in water = " + speedObject.getSpeedInWater());
}
if (medium =='S')
{
JOptionPane.showMessageDialog(null,"The speed of sound in a steel = " + speedObject.getSpeedInASteel());
}
}
}
class Speed
{
privatedouble distance;
// Constructor
public Speed(double dist)
{
distance = dist;
}
publicdouble getSpeedInAir()
{
return distance / 1100;
}
publicdouble getSpeedInWater()
{
return distance / 4900;
}
publicdouble getSpeedInASteel()
{
return distance / 16400;
}
}
> double distance;
Here the variable is declared. It has not been initialized at this point - it has no value yet.
> Speed speedObject = new Speed(distance);
Here you're trying to pass that unitialized variable to something. What do you expect it to do? Provide a value out of thin air for you?
You need to give distance a value because it is local.
I honestly don't know...im just kind of following some examples in my textbook....I very new to this coding. What do you suggest i do to fix the problem?
>What do you expect it to do? Provide a> value out of thin air for you?easy, give the guy a break.
Oh so i just need to give it a value of 0 basically....likedistance = 0; ?
> I honestly don't know...im just kind of following
> some examples in my textbook....I very new to this
> coding. What do you suggest i do to fix the problem?
Well you can answer that best. You need to use distance if your code. it serves some purpose and that purpose is it holds a value. the rest is down to you. Usually an initial value for ints is zero but you need to give it the value that works with your code.
Also, don't over use comments.
Like this
// Create Scanner object to read input
It is not needed. Save comments for other times when it is more worthy of an explantion. Use comments when it is more complex or not obvious to say why you are doing something not what you are doing.
> "variable might not have been initialized"
The compiler emits this diagnostic if at least one of the control flow
'paths' doesn't initialize/assign a value to a local variable before it's
being used/read. Here's a simple example:void possiblyWrong(int param) {
int localVar;
if ((param>>1) != (param/2)) localVar= 42;
System.out.println("localVar: "+localVar);
}
If the condition is false (which it never can be) the localVar is not
assigned and the next System.out.println() method would use an
uninitialized variable.
The compiler isn't smart enough to see that the condition can never
be false so it emits the error diagnostic you've seen in your code.
The remedy is simple though: simply give that variable an initial value,
as in:void possiblyWrong(int param) {
int localVar= 0;
...
Of course if there does exist a real control flow path where your variable
isn't initialized, the compiler diagnost message should be paid attention
to and the initialization fixed asap.
kind regards,
Jos
Okay guys...I got the initial problem solved but now I am coming across another problem i was hoping you could help me with. After entering the input into the dialog boxes, I want the results to be shown after calculating the speed of sound through each medium. After inputting the data, the showMessageDialog doesnt show. Any tips?
> inputMedium = JOptionPane.showInputDialog("Which medium are you calculating? \n (Please Enter A for 'Air', W for 'Water', or S for 'Steel')");
If the user enters "A", "W", or "S"...
> medium = Short.parseShort(inputMedium);
Then this will throw an exception, because it certainly isn't a numeric representation parseable to a short. Do you even know what a short is? It's an integer value with not quite the range of int.
And i am only using comments so much because this is a beginner's class and I am using them to remind myself....im not very advanced yet so bare with me
Sorry about that, I havent posted my updated code yet...i believe i got rid of that line completely.
Here is my updated code....i appreciate all the help you guys have been giving me!
public class VerheyenCh4Ex9
{
public static void main(String[] args)
{
String inputMedium, inputString;
short medium;
double distance = 0;
// Create Scanner object to read input
Scanner keyboard = new Scanner(System.in);
// Create speed object
Speed speedObject = new Speed(distance);
// Prompt user to enter "air", "water", or "steel"
inputMedium =
JOptionPane.showInputDialog("Which medium are you calculating? \n (Please Enter A for 'Air', W for 'Water', or S for 'Steel')");
inputString =
JOptionPane.showInputDialog("What is the distance (in feet) the speed is traveling?");
distance = Double.parseDouble(inputString);
if (inputMedium == "A")
{
JOptionPane.showMessageDialog(null, "The speed of sound in air = " + speedObject.getSpeedInAir());
}
else if (inputMedium == "W")
{
JOptionPane.showMessageDialog(null, "The speed of sound in water = " + speedObject.getSpeedInWater());
}
else if (inputMedium == "S")
{
JOptionPane.showMessageDialog(null, "The speed of sound in a steel = " + speedObject.getSpeedInASteel());
}
}
}
class Speed
{
private double distance;
// Constructor
public Speed(double dist)
{
distance = dist;
}
public double getSpeedInAir()
{
return distance / 1100;
}
public double getSpeedInWater()
{
return distance / 4900;
}
public double getSpeedInASteel()
{
return distance / 16400;
}
}
package distanceCalc;
import javax.swing.JOptionPane;
public class Distance {
public static void main(String[] args)
{
String medium;
String distanceString;
double distance = 0;
medium = JOptionPane.showInputDialog("Which medium are you calculating? \n (Please Enter A for 'Air', W for 'Water', or S for 'Steel')");
distanceString = JOptionPane.showInputDialog("What is the distance (in feet) the speed is traveling?");
distance = Double.parseDouble(distanceString);
Speed speed = new Speed(medium.toUpperCase(), distance);
JOptionPane.showMessageDialog(null, "The speed of sound in " + speed.getMedium() +" = " + speed.getSpeed());
}
}
class Speed
{
String medium;
Double distance;
// Constructor
public Speed(String mediumType, double distanceTravelled)
{
medium = mediumType;
distance = distanceTravelled;
}
public double getSpeed()
{
if (medium.equals("A"))
return distance/1100;
else if (medium.equals("W"))
return distance/4900;
else if (medium.equals("S"))
return distance/16400;
else
return 0.00;
}
public String getMedium()
{
if (medium.equals("A"))
return "Air";
else if (medium.equals("W"))
return "Water";
else if (medium.equals("S"))
return "Steel";
else
return "Null";
}
}
