ed holland
ed holland

Reputation: 21

UnknownFormatConversionException in a program that prints a percentage

I have to write a program where I put in baseball stats and it comes out with slugging % batting avg and re says the name of the player then put it on a sentinel loop. My current code is below. I'm trying to get it to work with just one before I turn it into a loop. When I run to test, I get UnknownFormatConversionException. What does it mean? How can I fix my code?

import java.util.Scanner;
public class bata
{
    public static void main(String[] args) throws Exception
    {
    double ba,sp,tb;
    int tri,dou,sin,hr,ab,hits;
    String name;
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Singles");
    sin = sc.nextInt();
    System.out.print("Enter Doubles");
    dou = sc.nextInt();
    System.out.print("Enter Triples");
    tri = sc.nextInt();
    System.out.print("Enter Homeruns");
    hr = sc.nextInt();
    System.out.print("Enter At bats");
    ab = sc.nextInt();
    System.out.print("Enter Player name");
    name = sc.nextLine();
    System.in.read();
    tb= (sin + (dou*2) + (tri *3) +(hr *4));
    hits = sin+dou+tri+hr;
    sp= tb/ab;
    ba= hits/ab;
    System.out.println(""+name);
    System.out.printf("Slugging % is %.3f\n", sp);
    System.out.printf("Batting avg os %.3f\n", ba);
    }
}

Upvotes: 2

Views: 399

Answers (2)

nook
nook

Reputation: 2396

UnknownFormatConversionException happens when you are expecting an integer and read a string from your scanner. It would be helpful if you could post your input file.

Also, escape the % sign using another %.

  System.out.printf("Slugging %% is %.3f\n", sp);

Upvotes: 0

Engineer
Engineer

Reputation: 48803

Escape % sign,by using double %%:

System.out.printf("Slugging %% is %.3f\n", sp);
//                           ^------------- escaping

Upvotes: 6

Related Questions