Results 1 to 4 of 4
  1. #1
    ArcherSam is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Unhappy 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)

    Java Code:
        public String toString(){
            return printf("%4s    %12s    %3s    $ %4.5", getItemID(), getItemName(), getInStore(), getPrice());
        }
    Sam

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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)

  3. #3
    ArcherSam is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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 02:03 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, glad to have helped. Please mark your thread solved with the thread tools at the top of the page.

Similar Threads

  1. printf
    By razzle in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 03:13 PM
  2. Printf problem!!!
    By pinar in forum New To Java
    Replies: 3
    Last Post: 11-06-2010, 10:44 PM
  3. Few errors with for loop/printf
    By ks1615 in forum New To Java
    Replies: 8
    Last Post: 03-12-2009, 10:47 PM
  4. Printf error
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 07:59 AM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:31 AM

Posting Permissions

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