Results 1 to 5 of 5
  1. #1
    cstampar is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default calling toString

    I have a toString method in a subclass:


    code/
    public String toString() {

    String print;

    print = String.format(" %.2f (%2d/% pa)",height, growthRate);
    return print;
    }
    /code

    and I need to make a call to this toString from another subclass so I can print the string but not sure how to do this.

    for instance, i currently have a variable "printing" that i initialized like this: code/ Tree printing = new Tree(); /code
    to call the toString because I think i've seen it done this way before and here is how i have it implemented:

    code/
    System.out.print(name);
    for (index = 0; index < MAX; index++) {
    System.out.println((index+1) + " : " + printing)
    /code

    but I'm not sure this is right. How do I call the toString to have it return the formatted string to print? Thank you!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: calling toString

    but I'm not sure this is right
    Did it print what you would expect if it were correct?

    You can call toString on any Object as you would any other defined method
    Using Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Passing an object to the println method causes the object's toString method to be inherently be called:
    Formatting (The Java™ Tutorials > Essential Classes > Basic I/O)

  3. #3
    cstampar is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: calling toString

    Thanks so much for your help!

    Instead of the toString i just decided to make another method within the same subclass for printing to return a formatted string but now this is causing other problems.

    Here is the display method in another subclass where the call to print method takes place:

    code/
    public void display() {

    System.out.print(name);
    for (index = 0; index < forestArray.length; index++) {
    System.out.println((index+1) + " : " + Tree.print());
    }
    }
    /code

    as you can see the print method is called at "Tree.print()" here is that method:

    code/

    public static String print() {

    String print;

    print = String.format("%.2f (%2d/% pa)",height, growthRate);
    return (print);
    }
    /code

    and when i run the program i get this error:

    code/
    nullException in thread "main" java.util.UnknownFormatConversionException: Conversion = 'p'
    at java.util.Formatter$FormatSpecifier.conversion(For matter.java:2606)
    at java.util.Formatter$FormatSpecifier.<init>(Formatt er.java:2634)
    at java.util.Formatter.parse(Formatter.java:2480)
    at java.util.Formatter.format(Formatter.java:2414)
    at java.util.Formatter.format(Formatter.java:2367)
    at java.lang.String.format(String.java:2769)
    at Tree.print(Tree.java:45)
    at Forest.display(Forest.java:26)
    at ForestryMain.menuMethod(ForestryMain.java:35)
    at ForestryMain.main(ForestryMain.java:15)
    /code

    it prints fine if i take out the "Tree.print()" call but with it i get this error. Can't figure out why.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: calling toString

    Please use the proper code formatting [code]Your code here[/code]

    You have not provided the full class definition, so you've left us to guess. My guess: you declared a static method which tries to access instance specific information. The method should not be declared static if you wish to accomplish this.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: calling toString

    nullException in thread "main" java.util.UnknownFormatConversionException: Conversion = 'p'
    The format string "%.2f (%2d/% pa)" includes a conversion that you don't mean (and the computer can't understand). Try %% for a percentage sign. (It's actually documented as the 5-th (percent) conversion in the API docs for Formatter.

Similar Threads

  1. toString
    By juanvalentinequintana in forum New To Java
    Replies: 2
    Last Post: 05-13-2011, 05:30 PM
  2. help with toString
    By teardrop3903 in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 03:39 AM
  3. toString
    By justin23 in forum New To Java
    Replies: 13
    Last Post: 05-02-2010, 03:44 PM
  4. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 09:52 AM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:32 PM

Tags for this Thread

Posting Permissions

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