Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: Why does this printf method throw an exception?

  1. #1
    mmirbekian is offline Member
    Join Date
    Jul 2016
    Posts
    17
    Rep Power
    0

    Default Why does this printf method throw an exception?

    Java Code:
    public class Phhhn {
    	
    	    public static void main(String[] args) {
    	    	  Scanner scanner = new Scanner(System.in);
    	          System.out.print("print first Double ");
    	          double input = scanner.nextDouble();
    	          System.out.print("print second Double ");
    	          double input2 = scanner.nextDouble();
    	          final double MILICONSTANT= 25.4;
    	          double first = MILICONSTANT * input;
    	          double second = MILICONSTANT * input2;
    	          
    	          System.out.printf(  "Milimeter dimensions are:  %.2f", first + " * " + second);
    	   }
    }
    Output:

    print first Double 8.5
    print second Double 11
    Milimeter dimensions are: Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%.2f'
    at java.util.Formatter.format(Formatter.java:2519)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at practice.Phhhn.main(Phhhn.java:18)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why does this printf method throw an exception?

    You are attempting to print a string to a floating point format specifier.

    Regards,
    Jim
    mmirbekian likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why does this printf method throw an exception?

    Does that error message go with the posted code? I get a different error message when I compile and execute the posted code.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    mmirbekian is offline Member
    Join Date
    Jul 2016
    Posts
    17
    Rep Power
    0

    Default Re: Why does this printf method throw an exception?

    I'm sorry I printed wrong output
    The main exception is:
    Exception in thread "main" java.util.IllegalFormatPrecisionException: 2
    at java.util.Formatter$FormatSpecifier.checkInteger(F ormatter.java:2984)
    at java.util.Formatter$FormatSpecifier.<init>(Formatt er.java:2729)
    at java.util.Formatter.parse(Formatter.java:2560)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at practice.Phhhn.main(Phhhn.java:18)

  5. #5
    mmirbekian is offline Member
    Join Date
    Jul 2016
    Posts
    17
    Rep Power
    0

    Default Re: Why does this printf method throw an exception?

    Quote Originally Posted by jim829 View Post
    You are attempting to print a string to a floating point format specifier.

    Regards,
    Jim
    Thank you Jim

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why does this printf method throw an exception?

    Heh! You may like my previous answer, but Norm was correct. And the above exception still does not seem to match. I get the following:

    Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion (Unknown Source)
    at java.util.Formatter$FormatSpecifier.printFloat(Unk nown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at examples.formatproblems.Phhhn.main(Phhhn.java:17)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    mmirbekian is offline Member
    Join Date
    Jul 2016
    Posts
    17
    Rep Power
    0

    Default Re: Why does this printf method throw an exception?

    Quote Originally Posted by jim829 View Post
    Heh! You may like my previous answer, but Norm was correct. And the above exception still does not seem to match. I get the following:

    Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion (Unknown Source)
    at java.util.Formatter$FormatSpecifier.printFloat(Unk nown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at examples.formatproblems.Phhhn.main(Phhhn.java:17)

    Regards,
    Jim
    awwww that's interesting !
    Does it depend on what program I use?
    I use eclipse
    So depending on what your exception is saying, It's a floating point and double problem
    But is there anyway to put it in try, catch without changing anything?

  8. #8
    mmirbekian is offline Member
    Join Date
    Jul 2016
    Posts
    17
    Rep Power
    0

    Default Re: Why does this printf method throw an exception?

    Quote Originally Posted by jim829 View Post
    Heh! You may like my previous answer, but Norm was correct. And the above exception still does not seem to match. I get the following:

    Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion (Unknown Source)
    at java.util.Formatter$FormatSpecifier.printFloat(Unk nown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at examples.formatproblems.Phhhn.main(Phhhn.java:17)

    Regards,
    Jim
    If I change my code, I can. Like this:(It works) but I wanna know howto deal w that exception without solving the problem this way..Will it work?
    Java Code:
    public class Phhhn {
    	
    	    public static void main(String[] args) {
    	    	  Scanner scanner = new Scanner(System.in);
    	          System.out.print("print first Double ");
    	          double input = scanner.nextDouble();
    	          System.out.print("print second Double ");
    	          double input2 = scanner.nextDouble();
    	          final double MILICONSTANT= 25.4;
    	          double first = MILICONSTANT * input;
    	          double second = MILICONSTANT * input2;
    	          
    	          DecimalFormat df = new DecimalFormat("#.##");      
    	          first = Double.valueOf(df.format(first));
    	          second = Double.valueOf(df.format(second));
    	          
    	          System.out.println(  "Milimeter dimensions are:  "+ first + " * " + second);
    	   }
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why does this printf method throw an exception?

    java.util.IllegalFormatConversionException: f != java.lang.String
    The format String must match the data that is being formatted. The data to format is a String so the format String needs to reference use the String formatting character. See the Formatter class API doc for a list of what characters to use in a format String.

    If you want to format three values: float, String, float (first, " * ", second) then there needs to be 3 data items separated by commas following the format String and 3 formatting Strings: one for float, one for String and one for float.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    mmirbekian is offline Member
    Join Date
    Jul 2016
    Posts
    17
    Rep Power
    0

    Thumbs up Re: Why does this printf method throw an exception?

    Quote Originally Posted by Norm View Post
    The format String must match the data that is being formatted. The data to format is a String so the format String needs to reference use the String formatting character. See the Formatter class API doc for a list of what characters to use in a format String.

    If you want to format three values: float, String, float (first, " * ", second) then there needs to be 3 data items separated by commas following the format String and 3 formatting Strings: one for float, one for String and one for float.
    Yupp ! got ya ! Thanksalot !

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why does this printf method throw an exception?

    For more information check the Format class for the different format specifiers.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How can we throw exception in java?
    By Java Example in forum Exception Examples
    Replies: 0
    Last Post: 09-25-2014, 09:38 PM
  2. Throw Exception in this code
    By eng_hyzoom in forum New To Java
    Replies: 9
    Last Post: 12-22-2010, 06:18 PM
  3. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 03:45 PM
  4. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 04:35 AM
  5. throw an exception
    By sfe23 in forum New To Java
    Replies: 3
    Last Post: 02-14-2009, 04:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •