-2

fix me plz. i get multiple error messages "variable airSpeed_km might not have been initialized" "variable width might not have been initialized" "variable length might not have been initialized"

import java.util.Scanner;
  public class V4_________1{

 public static void main (String args[])
 {
   Scanner keyboard = new Scanner(System.in);
   double KNOTS_TO_KMPHR;
   double airSpeed_km;
   double airSpeed_knots;
   double width;
   double length;
                            ***// need to do something in the main but not sure what exactly***
   airSpeed_knots = keyboard.nextDouble();
   System.out.println("what is your current airspeed in knots?");
   System.out.println("your current airspeed in km is: " + airSpeed_km  + "your holding pattern             width is: " + width + "your holding patter length is: " + length);
 }  
    public static double getAirSpeed(double airSpeed_knots, double KNOTS_TO_KMPHR, double    airSpeed_km)
{ 
  KNOTS_TO_KMPHR = 1.852;
  airSpeed_km = airSpeed_knots * KNOTS_TO_KMPHR ;
  return airSpeed_km;
}

public static double calcPatternWidth(double width, double airSpeed_km)
{ 
  width = (airSpeed_km) / (60 * Math.PI) * 2;  
  return width;
}  

public static double calcPatternLength(double airSpeed_km, double length)
{
  length = (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
  return length;
} 



}   
graeme
  • 1
  • possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Boann Oct 14 '14 at 16:04

3 Answers3

1

You declare:

   double airSpeed_km;

And after use it:

   System.out.println("your current airspeed in km is: " + airSpeed_km  + "your holding pattern             width is: " + width + "your holding patter length is: " + length);

without any assignment. So you get an error, you can prevent this by giving it a default value of 0 for example.

 double airSpeed_km = 0;

(same goes for your other errors)

Pienterekaak
  • 456
  • 2
  • 7
0

In Java, the compiler gets upset if a variable even MIGHT be used without a value. So it is best practice to always give a value to variables when you first declare them. Since you normally don't know the value a variable will have at the time of declaration, it is common practice to give it a value of zero.

So your declarations should look like this:

double KNOTS_TO_KMPHR=0;
double airSpeed_km=0;
double airSpeed_knots=0;
double width=0;
double length=0;

This will take care of all your "[] might not have been initialized" compiler errors.

Hopper06
  • 89
  • 2
  • 10
0

Your code is in correct. Your cannot set variable when you pass them into a function. See both approaches and understand what going on. You can do this:

 public static void main (String args[])
 {
   Scanner keyboard = new Scanner(System.in);
   double KNOTS_TO_KMPHR=1.852;
   double airSpeed_knots;
   System.out.println("what is your current airspeed in knots?");
   airSpeed_knots = keyboard.nextDouble();

   System.out.println("your current airspeed in km is: " + getAirSpeed(airSpeed_knots)  + "your holding pattern             width is: " + calcPatternWidth(getAirSpeed(airSpeed_knots)) + "your holding patter length is: " + calcPatternLength(getAirSpeed(airSpeed_knots));
 }  
 public static double getAirSpeed(double airSpeed_knots)
{ 
    return airSpeed_knots * KNOTS_TO_KMPHR ;
}

public static double calcPatternWidth(double airSpeed_km)
{ 
  return (airSpeed_km) / (60 * Math.PI) * 2;  
}  

public static double calcPatternLength(double airSpeed_km)
{
  return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
} 

Or do this if you want to set variables:

 public static void main (String args[])
 {
   Scanner keyboard = new Scanner(System.in);
   double KNOTS_TO_KMPHR=1.852;
   double airSpeed_knots;
   System.out.println("what is your current airspeed in knots?");
   airSpeed_knots = keyboard.nextDouble();

   double airSpeed_km=getAirSpeed(airSpeed_knots);
   double width=calcPatternWidth(airSpeed_km);
   double length= calcPatternLength(airSpeed_km);

   System.out.println("your current airspeed in km is: " + airSpeed_km  + "your holding pattern             width is: " + width + "your holding patter length is: " + length);
 }  
 public static double getAirSpeed(double airSpeed_knots)
{ 
    return airSpeed_knots * KNOTS_TO_KMPHR ;
}

public static double calcPatternWidth(double airSpeed_km)
{ 
  return (airSpeed_km) / (60 * Math.PI) * 2;  
}  

public static double calcPatternLength(double airSpeed_km)
{
  return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
} 
StackFlowed
  • 6,664
  • 1
  • 29
  • 45