Home > Blockchain >  Exception in thread "main" java.util.IllegalFormatConversionException: f !=
Exception in thread "main" java.util.IllegalFormatConversionException: f !=

Time:10-01

when running this program I am having this problem in my command line, I need to use only printf and need the total surface area and cost to 2 decimal places

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at MartianHouses.main(MartianHouses.java:28)

 import java.util.Scanner;
public class MartianHouses {
    public static void main(String [] args) {
        Scanner input = new Scanner(System.in); //create scanner
        System.out.println("Enter the settles's name: "); // prompt user for name
        String settlerName = input.nextLine(); //
        System.out.println("Enter the length of a side of the house: ");
        double oneSideOfHouse = input.nextInt();
        double floorArea;
        double roofArea;
        double areaOfOuterWalls;
        double totalSurfaceArea;
        double costOfHouse;
        if (oneSideOfHouse != 0) {
            floorArea = (2 * (Math.pow(oneSideOfHouse, 2))) * (1   (Math.pow(2, 0.5)));
            roofArea = (2 * (Math.pow(oneSideOfHouse, 2))) * (1   (Math.pow(2, 0.5)));
            areaOfOuterWalls = 8 * 12 * oneSideOfHouse;
            totalSurfaceArea = floorArea   areaOfOuterWalls   roofArea;
            costOfHouse = 14.50 * totalSurfaceArea;
            System.out.printf(settlerName   " has a house surface area of "   ("%.2f"), totalSurfaceArea   " and cost of "   ("%.2f), costOfHouse);
        } 

    }
}

CodePudding user response:

The order of the attributes is wrong. Try the following:

Scanner input = new Scanner(System.in); //create scanner
System.out.println("Enter the settles's name: "); // prompt user for name
String settlerName = input.nextLine(); //
System.out.println("Enter the length of a side of the house: ");
double oneSideOfHouse = input.nextInt();
double floorArea;
double roofArea;
double areaOfOuterWalls;
double totalSurfaceArea;
double costOfHouse;
if (oneSideOfHouse != 0) {
    floorArea = (2 * (Math.pow(oneSideOfHouse, 2))) * (1   (Math.pow(2, 0.5)));
    roofArea = (2 * (Math.pow(oneSideOfHouse, 2))) * (1   (Math.pow(2, 0.5)));
    areaOfOuterWalls = 8 * 12 * oneSideOfHouse;
    totalSurfaceArea = floorArea   areaOfOuterWalls   roofArea;
    costOfHouse = 14.50 * totalSurfaceArea;
    System.out.printf(settlerName   " has a house surface area of "   ("%.2f")   " and cost of "   ("%.2f"), totalSurfaceArea, costOfHouse);
}

With your code System.out.printf(settlerName " has a house surface area of " ("%.2f"), totalSurfaceArea " and cost of " ("%.2f"), costOfHouse); you are trying to format the second argument totalSurfaceArea " and cost of " ("%.2f") with the previous ("%.2f"), hence the error.

  •  Tags:  
  • java
  • Related