Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 8th, 2013, 02:48 PM
Person33 Person33 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 17 Person33 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old March 8th, 2013, 02:57 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,040 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 36 m 54 sec
Reputation Power: 346
Quote:
an issue with some conversion

Please copy the full text of the error message and paste it here.

Reply With Quote
  #3  
Old March 8th, 2013, 07:44 PM
burakaltr burakaltr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 46 burakaltr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old March 11th, 2013, 03:11 PM
Person33 Person33 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 17 Person33 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old March 11th, 2013, 03:13 PM
Person33 Person33 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 17 Person33 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old March 11th, 2013, 03:13 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,040 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 36 m 54 sec
Reputation Power: 346
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.

Reply With Quote
  #7  
Old March 12th, 2013, 02:45 PM
Person33 Person33 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 17 Person33 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old March 12th, 2013, 03:15 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,040 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 36 m 54 sec
Reputation Power: 346
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.

Reply With Quote
  #9  
Old March 13th, 2013, 02:34 PM
Person33 Person33 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 17 Person33 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Printf

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap