Search code examples
javatemplatesprintfmalformed

Malformed format string issue with a printf template


The assignment is to prompt for the radius of a circle, to calculate the area, and then print it using printf. I cannot get the error to go away after doing plenty of digging. Please, tell me what I'm doing wrong. The yellow error line appears below the printf template: "Area of the Circle is (%f)^2*3.14=%f.." -- This template was given by the professor.

    System.out.print("Enter the radius of a circle: ");
    float circleRadius = input.nextFloat();

    float circleArea = 3.14F * circleRadius * circleRadius;

    System.out.printf("Area of the Circle is (%f)^2*3.14=%f\n", circleArea);

Solution

  • You need to match each format specifier with an argument

    System.out.printf("Area of the Circle is (%f)^2*3.14=%f\n", circleRadius, circleArea);