The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Printf
Discuss Printf in the Java Help forum on Dev Shed. Printf Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 8th, 2013, 02:48 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 17
Time spent in forums: 2 h 45 m 6 sec
Reputation 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.
|

March 8th, 2013, 02:57 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | an issue with some conversion |
Please copy the full text of the error message and paste it here.
|

March 8th, 2013, 07:44 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 46
Time spent in forums: 9 h 58 m 48 sec
Reputation 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.
|

March 11th, 2013, 03:11 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 17
Time spent in forums: 2 h 45 m 6 sec
Reputation Power: 0
|
|
|
I have my main method in a separate class, so that's not the problem, but thanks.
|

March 11th, 2013, 03:13 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 17
Time spent in forums: 2 h 45 m 6 sec
Reputation 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.
|

March 11th, 2013, 03:13 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | .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.
|

March 12th, 2013, 02:45 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 17
Time spent in forums: 2 h 45 m 6 sec
Reputation 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.
|

March 12th, 2013, 03:15 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | 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.
|

March 13th, 2013, 02:34 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 17
Time spent in forums: 2 h 45 m 6 sec
Reputation Power: 0
|
|
Thanks. I found my error. I had written "%10" instead of "%10s". Now the program works. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|