Home > other >  After running code, I received the Exception in thread "main" java.util.UnknownFormatConve
After running code, I received the Exception in thread "main" java.util.UnknownFormatConve

Time:11-08

The problem is " Conversion = '-'". The source code is listed below. This is a program used to caculating the value of Coefficent A, B, and C based on the quadratic function.

import java.util.Scanner;

public class RootsTestHamer {
    public static void main(String[] args) {
        //declare variables
        double a, b, c;
        double r1, r2;
        
    Scanner input = new Scanner(System.in); // Create a Scanner
    
        // prompts user to enter values for coefficents A, B, and C
        System.out.print("Enter the A coefficent:");
         a = input.nextDouble();
        System.out.print("Enter the B coefficent:");
         b = input.nextDouble();
        System.out.print("Enter the C coefficent:");
         c = input.nextDouble();

        // calucating roots
        r1 = ( Math.pow(b,2) - (4 * a * c));
        r1 = (-b   (Math.sqrt(r1))) / (2 * a);

        r2 = ( Math.pow(b,2) - (4 * a * c));
        r2 = (-b - (Math.sqrt(r2))) / (2 * a);

        // calls the method and determines # of roots
        double r = roots (a, b, c);

        // if statements and display
        if (r > 1) {
            System.out.printf("%.0f, %.of, and %.0f coefficients have %.0f real roots:",a, b, c, r);
            System.out.printf("\n\tR1 = %.2f",r1);
            System.out.printf("\n\tR1 = %.2f",r2);
        }

        else if (r == 1) 
        {
        System.out.printf("%.0f, %.of, and %.0f coefficients have %.0f real roots:",a, b, c, r);
        System.out.printf("\n\tR1 = %.2f",r1);
        }

        else if (r == 0) {
        System.out.printf("%.0f, %.of, and %.0f coefficients have %.0f real roots:",a, b, c);
        }
}

        //user - defined root method
    public static double roots(double a, double b, double c) {
        double roots = 0;

        //number of roots
        if (Math.pow(b,2) - (4 * a * c) > 0) {
            roots = 2;
        }
        else if (Math.pow(b,2) - (4 * a * c) == 0) {
            roots = 1;
        }
        else if (Math.pow(b,2) - (4 * a * c) < 0) {
            roots = 0;
        }       

            return roots;
    }
}

I attempted to run the code. After entering the value 1, 2,3 for coefficents A, B,and C, the code returned Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '-'

CodePudding user response:

The issue is your print statements:

    System.out.printf("%.0f, %.of, and %.0f coefficients have %.0f real roots:",a, b, c, r);
    ...
    System.out.printf("%.0f, %.of, and %.0f coefficients have %.0f real roots:",a, b, c, r);
    ...
    System.out.printf("%.0f, %.of, and %.0f coefficients have %.0f real roots:",a, b, c);

I think you have a typo in all of these: %.of should be %.0f as they are elsewhere in the string.

Also the last print statement needs to have all four arguments a, b, c, r like the first two do.


In general, it is always helpful to look at the stack trace java provides on an error to see what line of the code you wrote is causing the issue

CodePudding user response:

You have a typo in your messages, the second parameter "of" instead of "0f", and r == 0 print statement requires 4 parameters you have only given 3. Check below.

if (r > 1) {
            System.out.printf("%.0f, %.0f, and %.0f coefficients have %.0f real roots:",a, b, c, r);
            System.out.printf("\n\tR1 = %.2f",r1);
            System.out.printf("\n\tR1 = %.2f",r2);
        }

        else if (r == 1)
        {
            System.out.printf("%.0f, %.0f, and %.0f coefficients have %.0f real roots:",a, b, c, r);
            System.out.printf("\n\tR1 = %.2f",r1);
        }

        else if (r == 0) {
            System.out.printf("%.0f, %.0f, and %.0f coefficients have %.0f real roots:",a, b, c, r);
        }
  • Related