0

Okay so I'm trying to make a change calculator, and I'm getting some error codes telling me my variables aren't initialized, not really sure how to fix it.

Thanks, I'll provide any more details if needed.

Basically, all it is doing right now is calculating the cost of the product with tax, and then taking input for the different denominations of money that can pay with. "100" is the number that you can type to stop using new denominations. Thanks.

  //**********************************************************
  // Assignment: Operations on numbers 12 & 9
  //
  // Description: This finds the sum, difference, product, and quotient (With remainder) of 12 and 9
  //
  // Author: Rowen Paredes-Morris
  //
  // Date Start: 18/02/2016
  // Date Completed: 18/02/2016
  //
  // Completion time: 20 minutes
  //
  // Honor Code: I pledge that this program represents my own
  //   program code. I received help from Mr. Bozz
  //*********************************************************

  //  A program to add two numbers.

  import java.util.Scanner;

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

      // Step 1 - Declaring variables
        Scanner input = new Scanner(System.in);
        int loonie;
        int toonie;
        int quarter;
        int dime;
        int nickel;
        int penny;
        double change;
        double priceNoTax;
        double priceWithTax;
        int firstDenomination;
        int secondDenomination;
        int thirdDenomination;
        int fourthDenomination;
        int fifthDenomination;
        int moneyInserted;

      // Step 2 - (Input) Assigning values to variables

        System.out.print("What is the price of your purchase before tax?");
        priceNoTax = input.nextDouble();
        priceWithTax = priceNoTax*1.13;
        System.out.print("The price with tax is $"+priceWithTax);

        System.out.print("\n\nWhat is the first denomination you will you be paying with? (1, 2, 5, 10, 20, 50");

        firstDenomination = input.nextInt();

        while (firstDenomination != 1 && firstDenomination != 2 && firstDenomination != 5 && firstDenomination != 10 && firstDenomination != 20 && firstDenomination != 50 && firstDenomination != 100)
        {
           System.out.print("Please enter a valid denomination");
           firstDenomination = input.nextInt();
        }

        System.out.print("If you are paying with another denomination, type it in. If not, type \"100\"");

        secondDenomination = input.nextInt();
        while (secondDenomination == firstDenomination || secondDenomination != 1 && secondDenomination != 2 && secondDenomination != 5 && secondDenomination != 10 && secondDenomination != 20 && secondDenomination != 50 && secondDenomination !=100)
        {
           System.out.print("Please enter a new or valid denomination");
           secondDenomination = input.nextInt();
        }

        if (secondDenomination != 100 )
        {
           System.out.print("Your second denomination is $"+secondDenomination);
           System.out.print("\n\nIf you are paying with another denomination, type it in. If not, type \"100\"");
           thirdDenomination = input.nextInt();

           while (thirdDenomination == firstDenomination || thirdDenomination == secondDenomination || thirdDenomination != 1 && thirdDenomination != 2 && thirdDenomination != 5 && thirdDenomination != 10 && thirdDenomination != 20 && thirdDenomination != 50 && thirdDenomination !=100)
           {
              System.out.print("Please enter a new or valid denomination");
              thirdDenomination = input.nextInt();
           }

           if (thirdDenomination != 100)
           {
              System.out.print("If you are paying with another denomination, type it in. If not, type \"100\"");
              fourthDenomination = input.nextInt();

              while (fourthDenomination == firstDenomination || fourthDenomination == secondDenomination || fourthDenomination == thirdDenomination || fourthDenomination != 1 && fourthDenomination != 2 && fourthDenomination != 5 && fourthDenomination != 10 && fourthDenomination != 20 && fourthDenomination != 50 && fourthDenomination !=100)
              {
                 System.out.print("Please enter a new or valid denomination");
                 fourthDenomination = input.nextInt();
              }

              if (fourthDenomination != 100)
              {
                 System.out.print("If you are paying with another denomination, type it in. If not, type \"100\"");
                 fifthDenomination = input.nextInt();

                 while (fifthDenomination == firstDenomination || fifthDenomination == secondDenomination || fifthDenomination == thirdDenomination || fifthDenomination == fourthDenomination || fifthDenomination != 1 && fifthDenomination != 2 && fifthDenomination != 5 && fifthDenomination != 10 && fifthDenomination != 20 && fifthDenomination != 50 && fifthDenomination !=100)
                 {
                    System.out.print("Please enter a new or valid denomination");
                    fifthDenomination = input.nextInt();
                 }        
              }
           }
        }

        if (fifthDenomination != 100)
        {
           System.out.print("First denomination:   $"+firstDenomination);
           if (secondDenomination != 100)
           {
              System.out.print("\nSecond denomination:  $"+secondDenomination);

              if (thirdDenomination !=100)
              {
                 System.out.print("\nThird denomination:   $"+thirdDenomination);

                 if (fourthDenomination !=100)
                 {         
                    System.out.print("\nFourth denomination:  $"+fourthDenomination);

                    if(fifthDenomination !=100)
                    {
                       System.out.print("\nFifth denomination:   $"+fifthDenomination);
                    }
                 }
              }
           }
        }



        }   
     }

Errors:

      ChangeCalculator.java:107: variable fifthDenomination might not have been initialized
       if (fifthDenomination != 100)
           ^
 ChangeCalculator.java:114: variable thirdDenomination might not have been initialized
             if (thirdDenomination !=100)
                 ^
 ChangeCalculator.java:118: variable fourthDenomination might not have been initialized
                if (fourthDenomination !=100)
  • 1
    The error is actually telling you exactly what's wrong. The variables aren't initialized before you're trying to compare their values. You need to initialize them to whatever you think they should be set to. – Hypino Feb 23 '16 at 18:11
  • 1
    initialize all int to 0 and double to 0.0 – yogidilip Feb 23 '16 at 18:13

0 Answers0