Jake
Jake

Reputation: 313

Want to loop but variable is not initialized

My problem starts at the first do-while loop. I want it to ask "Please enter an angle between 0 an 90: " and if the user does input a number between 0 and 90, they have to input the amount of gun powder. Their inputs will go through the physics calculations and if their outcome is not "It's a hit!" then I want them to go back and input the angle and gunpowder again. But the thing is at the

while (distance - distance2 != 0 || distance - distance2 != 1 || distance2 - distance != 1); System.out.println("It's a hit!");

The error says: variable distance2 might not have been initialized.


    import java.util.*;
    import java.text.*;
    public class BombsAway {
    private static double ZERO_THOUSAND = 1000;
    private static double KG_TO_VELOCITY = 50;
    private static double GRAVITY = -9.81;
    public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      DecimalFormat threeDec = new DecimalFormat("#.##");
      Random gen = new Random();
      BombsAway distanceAn = new BombsAway();
      boolean invalidInput = true;
      double distance, distance2;

      System.out.println("Please enter a positive integer seed value: ");
      while(invalidInput) {
         try {
            int seedValue = Integer.valueOf(input.nextLine());
            if (seedValue <= 0) {
               System.out.println("Please enter a positive integer seed value:"
                + " ");
            } 
            else {
               distance = gen.nextDouble() * ZERO_THOUSAND;
               System.out.println("That target is " + threeDec.format(distance) 
                + "m away.");  
               do {
               System.out.println("Please enter an angle between 0 and 90 " +
                "degrees: ");
               while (invalidInput) {
                  try {
                     double angle = Double.valueOf(input.nextLine());
                     if (angle < 0 || angle > 90) {
                        System.out.println("Please enter an angle between " +
                         "0 and 90 degrees: ");
                     } 
                     else {
                        System.out.println("Please enter the amount of " + 
                         "gunpowder in kilograms: ");
                        try {
                           double gunpowder = 
                            Double.valueOf(input.nextLine());
                           //physics part
                           double initialV = gunpowder * KG_TO_VELOCITY;
                           double vX = initialV * Math.cos(angle);
                           double vY = initialV * Math.sin(angle);
                           double airBorn = (-vY - vY) / GRAVITY;
                           distance2 = (vX * airBorn);

                           if (distance > distance2) {
                              double finalDist = distance - distance2;
                              System.out.println("It's " +
                               threeDec.format(finalDist) + "m short.");
                           }
                           else if (distance < distance2) {
                              double finalDist = distance2 - distance;
                              System.out.println("It's " + 
                               threeDec.format(finalDist) + "m long.");
                           }
                        }
                        catch(NumberFormatException e) {
                           System.out.println("Please enter the amount of " +
                            "gunpowder in kilograms: ");
                        }
                     }
                  }
                  catch(NumberFormatException e) {
                      System.out.println("Please enter an angle between " +
                       "0 and 90 degrees: ");
                  }
               }
            }while (distance - distance2 != 0 || distance - distance2 != 1
              || distance2 - distance != 1);
                System.out.println("It's a hit!");
            }
         }
         catch(NumberFormatException e) {
            System.out.println("Please enter a positive integer seed value: ");
         }
      }
   }
}

Upvotes: 0

Views: 824

Answers (2)

Ogre Psalm33
Ogre Psalm33

Reputation: 21946

The distance2 variable is initialized in a try/catch block. It's possible an exception could be thrown, and land the flow of control in the catch block before distance2 is initialized. After the catch block, distance2 is used in a calculation at the end of the while loop, so yes: it might not be initialized at this line:

while (distance - distance2 == 0 || distance - distance2 == 1
      || distance2 - distance == 1)

if a NumberFormatException was thrown when you are reading the gunpowder or angle values from input. There is also an if condition (bounds check on angle) that could leave distance2 unset.

Upvotes: 2

exexzian
exexzian

Reputation: 7890

In your code - distance2 is initialized inside one of the conditional blocks (in else block inside while loop) there may be cases when your while or else condition will not be fulfilled and in that case distance2 is left un-initialized and Local variables cannot be used without initialization so simply initialize that distance2 with any meaningful or required default value as:

double distance2 = 0.0; or with some specific value that your code logic works on

Upvotes: 4

Related Questions