Results 1 to 4 of 4
- 05-10-2011, 12:44 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Overrding toString() Containing printf()
Hey, i'm trying to format my output by using the printf(). Why won't the code below work?
Error: cannot find symbol - mehtod printf(java.lang.String, java.lang.String, java.lang.String, int, double)
SamJava Code:public String toString(){ return printf("%4s %12s %3s $ %4.5", getItemID(), getItemName(), getInStore(), getPrice()); }
- 05-10-2011, 12:47 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
printf is used to display info with System.out.printf. You want a formatted string to be returned. Check out the static method String.format
String (Java Platform SE 6)
- 05-10-2011, 12:56 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Thanks sunde887, the String.format() "compiled with no errors!" :)
Here's the code that gave the solution:
Java Code:String s = String.format("%4s %12s %3s $ %4.5s", getItemID(), getItemName(), getInStore(), getPrice()); return s;Last edited by ArcherSam; 05-10-2011 at 01:03 AM.
- 05-10-2011, 12:58 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are welcome, glad to have helped. Please mark your thread solved with the thread tools at the top of the page.
Similar Threads
-
printf
By razzle in forum New To JavaReplies: 4Last Post: 11-12-2010, 02:13 PM -
Printf problem!!!
By pinar in forum New To JavaReplies: 3Last Post: 11-06-2010, 09:44 PM -
Few errors with for loop/printf
By ks1615 in forum New To JavaReplies: 8Last Post: 03-12-2009, 09:47 PM -
Printf error
By ks1615 in forum New To JavaReplies: 4Last Post: 03-10-2009, 06:59 AM -
printf
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks