Thread: Printf

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Printf


    I'm new to using java and I'm having trouble with this code. Eclipse tells me there is an issue with some conversion. I have no idea what that means, but I'm pretty sure it has to do with printf. Here is the code:

    package test;
    import java.util.*;
    public class TestPrintF {
    private int number;
    Scanner scr = new Scanner(System.in);
    public TestPrintF(){

    number = 0;
    }

    public void changeNumber(){
    System.out.print("Enter an integer: ");
    number = scr.nextInt();
    }
    public void output(){
    System.out.printf("%10", number);
    }
    }


    In this specific example, I know there are ways to do it without using printf, but I'm trying to work out how to use printf specifically.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    an issue with some conversion
    Please copy the full text of the error message and paste it here.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    47
    Rep Power
    0
    Code:
    import java.util.Scanner;
    
    public class TestPrintF {
     private int number;
     Scanner scr = new Scanner(System.in);
     public TestPrintF(){
    
     number = 0;
     }
    
     public void changeNumber(){
     System.out.print("Enter an integer: ");
     number = scr.nextInt();
     }
     public void output(){
     System.out.printf("%10", number);
     }
     }
    You are missing the main method.
    Be sure to declare as

    Code:
    public static void main(String[]args)
    which will be the start of execution of your Application.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    I have my main method in a separate class, so that's not the problem, but thanks.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    This is the full error message:

    Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '1'
    at java.util.Formatter.checkText(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at test.TestPrintF.output(TestPrintF.java:16)
    at test.Main.main(Main.java:11)

    I'm afraid it makes absolutely no sense to me.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    .UnknownFormatConversionException: Conversion = '1'
    The formatting in the printf method does not recognize the format control characters you are using. See the Formatter class's documentation in the API doc for a full description on how to code format strings.
    The API doc
    Last edited by NormR; March 11th, 2013 at 03:21 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    I went to the API; I didn't see anywhere that Formatter could actually use printf. Concerning the error message I got, the API said, "Any characters not explicitly defined as conversions, date/time conversion suffixes, or flags are illegal and are reserved for future extensions. Use of such a character in a format string will cause an UnknownFormatConversionException." But I'm wondering if there's actually a different class or package I should be importing, or if I'm just not using printf correctly. Formatter's out() method didn't go into a System.out.printf section.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    I'm just not using printf correctly.
    The printf() method is ok to use. The problem is that the printf() method is complaining that the format characters in the String passed to the printf() method are wrong. The correct usage of format characters is described in the API doc for the Formatter class.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0
    Thanks. I found my error. I had written "%10" instead of "%10s". Now the program works.

IMN logo majestic logo threadwatch logo seochat tools logo