Home > database >  Java - The Local variable may not have been initialized - New to programming question
Java - The Local variable may not have been initialized - New to programming question

Time:01-29

I'm taking Elementary Programming and I'm trying to get this code to work, and I can only use the first 3 chapters worth of the Java Foundations book, and the book is very complicated and unclear on this part. I can't even tell you if I'm approaching this the right way, but I've been working on it for over a week now and just can't get it.

This is supposed to let the user input the houses cost, tax rate over 5 years, and fuel rate over 5 years for 3 houses, then print them in a side by side table across 4 lines.

    import java.util.Scanner;
    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    
    
    
    public class houseCost {
    

    public static void main(String[] args) 
        
    
        

    {
        
        
    
    double costHouse1, costHouse2, costHouse3, fuelHouse1, fuelHouse2, fuelHouse3, finalHouse1, finalHouse2, finalHouse3, finalFuel1, finalFuel2, finalFuel3, finalTax1, finalTax2, finalTax3, taxHouse1, taxHouse2, taxHouse3;
    
    {
    Scanner scan = new Scanner(System.in);
    
    NumberFormat fmt1, fmt2, fmt3 = NumberFormat.getCurrencyInstance();
    
    
    
    
    System.out.print("Enter the value of the first house:");
    costHouse1 = scan.nextDouble();
    
    System.out.print("Enter the fuel cost for the first house:");
    fuelHouse1 = scan.nextDouble();
    
    System.out.print("Enter the tax rate of the first house:");
    taxHouse1 = scan.nextDouble();
    
    System.out.print("Enter the value of the second house:");
    costHouse2 = scan.nextDouble();
    
    System.out.print("Enter the fuel cost of the second house:");
    fuelHouse2 = scan.nextDouble();
    
    System.out.print("Enter the tax rate of the second house:");
    taxHouse2 = scan.nextDouble();
    
    System.out.print("Enter the value of the third house:");
    costHouse3 = scan.nextDouble();
    
    System.out.print("Enter the fuel cost of the third house:");
    fuelHouse3 = scan.nextDouble();
    
    System.out.print("Enter the tax rate of the third house:");
    taxHouse3 = scan.nextDouble();
    
    finalTax1 = ((costHouse1 * taxHouse1) * 5);
    finalTax2 = ((costHouse2 * taxHouse2) * 5);
    finalTax3 = ((costHouse3 * taxHouse3) * 5);
    
    finalFuel1 = (fuelHouse1 * 5);
    finalFuel2 = (fuelHouse2 * 5);
    finalFuel3 = (fuelHouse3 * 5);
    
    
    finalHouse1 = costHouse1   finalTax1   finalFuel1;
    finalHouse2 = costHouse2   finalTax2   finalFuel2;
    finalHouse3 = costHouse3   finalTax3   finalFuel3;
    
    
    
        
    
    System.out.printf("Initial House Cost\tAnnual Fuel Cost\tTaxes\t\tTotal Cost\n");
    System.out.printf("\t]\t\t\t]\t\t]\t\t]\n", costHouse1, finalFuel1, finalTax1, finalHouse1);
    System.out.printf("\t]\t\t\t]\t\t]\t\t]\n", costHouse2, finalFuel2, finalTax2, finalHouse2);
    System.out.printf("\t]\t\t\t]\t\t]\t\t]\n", costHouse3, finalFuel3, finalTax3, finalHouse3);
    
    
    
            
}}}

If I run the program as is (or using DecimalFormat as opposed to NumberFormat), I can enter all the numbers and as soon as the 9th is entered, I get

"Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4442) at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2963) at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2918) at java.base/java.util.Formatter.format(Formatter.java:2689) at java.base/java.io.PrintStream.format(PrintStream.java:1209) at java.base/java.io.PrintStream.printf(PrintStream.java:1105) at houseCost.main(houseCost.java:74)"

What I have read up on is that the NumberFormat and DecimalFormat are not variables to store data, but how to format it. I MUST use one or both of these in the project. I also have to format the output to look like a table which I think I did right with the printf.

I keep getting differing errors seen below. Where the variable initialization comes in is if I switch the bottom lines of code to something like this:

"

System.out.printf("\t]\t\t\t]\t\t]\t\t]\n", costHouse1, finalFuel1, finalTax1, fmt1.format(finalHouse1));

"

So, the ultimate question is, how do I resolve the error and take all the inputted numbers, put them through their calculations, and output them in ("0.##") format? Am I close? Have I really bothced this?

Thanks for your help!

CodePudding user response:

Since you're using double variables, you have to ensure the correct string format in printf(). We generally use %d od %i to represent integer values, and we use %f (f as in floating point) to represent floating point values.

CodePudding user response:

To format the values in your output, using NumberFormat class. Here's how to do it with your current code:

NumberFormat fmt = NumberFormat.getCurrencyInstance();

To display the formatted currency values in your output, you can use the printf method and include the %s placeholder in your format string, then pass the formatted currency values using the fmt.format() method.

System.out.printf("\t%s\t\t\t%s\t\t%s\t\t%s\n",
                    fmt.format(costHouse1),
                    fmt.format(finalFuel1),
                    fmt.format(finalTax1),
                    fmt.format(finalHouse1));

Regarding the issue with your current code, you are using the %d placeholder for the printf method, which is used for integers. Since your variables are of type double, you should use the %f placeholder instead.

System.out.printf("\t_\t\t\t_\t\t_\t\t_\n",
                        costHouse1,
                        finalFuel1,
                        finalTax1,
                        finalHouse1);
  • Related