Results 1 to 8 of 8
  1. #1
    arri3oi is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default Why isn't my toString method working

    Java Code:
    public void printCapacity() {
    		System.out.println("The list capcity is " + (capacity).toString());
    	}
    I created toString method in another class but when I call it, it's not working and its giving me an error mesg.

    When I write my code like this:

    Java Code:
    public void printCapacity() {
    		System.out.println("The list capcity is " + (capacity) + toString());
    	}
    When I run it, it prints weird. (if that makes any sense) I'm new at this so forgive me for my stupidity.

    Arri

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why the parenthesis around capacity? If the variable is called capacity, then you simply call toString() on the variable. But this being said, you don't even need to call toString from within a println method call since it will automatically be called. I think you need to tell us more.

  3. #3
    arri3oi is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    This program involves use of arrays. My professor wants us to incorporate the toString method with this program. But I didn't see the need for it because it prints perfectly without it, and truthfully I just didn't know how to do it. He grades hard and if in the requirements, it has to be there. And I don't know why I put the parens around capacity. I was following a example in my book. Hope that helps

    Arri

  4. #4
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by arri3oi View Post
    This program involves use of arrays. My professor wants us to incorporate the toString method with this program. But I didn't see the need for it because it prints perfectly without it, and truthfully I just didn't know how to do it. He grades hard and if in the requirements, it has to be there. And I don't know why I put the parens around capacity. I was following a example in my book. Hope that helps

    Arri
    The following two examples does exactly the same:
    Java Code:
    System.out.println("The capacity is: " + capacity);
    Java Code:
    System.out.println("The capacity is: " + capacity.toString());
    So basically what I think Fubarable is saying is that we need to know exactly how the toString() is coded.

  5. #5
    arri3oi is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Ok I see.

    Java Code:
    public String toString() {
    		return name + "\n" + description + "\n" + health + "\n" + strength;
    	}
    This works and prints fine:

    Java Code:
    	public String displayAsString() {
    		return "----------------------------------\n" + name + "\n"
    				+ description + "\nHealth: " + health + " Strength: "
    				+ strength + "\n----------------------------------\n";
    	}
    Java Code:
    		System.out.println("Max is found at index = " + x + "\n"
    				+ list[x].displayAsString());
    		return x;
    	}
    Arri

  6. #6
    arri3oi is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    		System.out.println("Max is found at index = " + (x).toString() + "\n"
    				+ list[x].displayAsString());
    		return x;
    	}
    It gives me an error.

  7. #7
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Quote Originally Posted by arri3oi View Post
    Ok I see.

    Java Code:
    public String toString() {
    		return name + "\n" + description + "\n" + health + "\n" + strength;
    	}
    This works and prints fine:

    Java Code:
    	public String displayAsString() {
    		return "----------------------------------\n" + name + "\n"
    				+ description + "\nHealth: " + health + " Strength: "
    				+ strength + "\n----------------------------------\n";
    	}
    Java Code:
    		System.out.println("Max is found at index = " + x + "\n"
    				+ list[x].displayAsString());
    		return x;
    	}
    Arri
    You said you got an error message in the first call of the toString(), is it a syntax-error? - or do you mean an exception? (runtime-error).
    In any case, can you post it?

    EDIT: I'm only guessing here but isn't "x" an 'int'? As far as I'm aware, toString()-methods cannot be called on primitive types (all the types with only lower case letters; int, float, boolean etc. while e.g. String isn't one) and your toString()-method should be located in the class, that you're variable "capacity" (or whatever you're trying to print) is an object of.

    All classes that extends Object (they do that by default) has a toString()-method by default although it may look weird to the user - so writing one yourself overrides that.
    Last edited by Muskar; 12-13-2010 at 05:04 AM.

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by arri3oi View Post
    Java Code:
    		System.out.println("Max is found at index = " + (x).toString() + "\n"
    				+ list[x].displayAsString());
    		return x;
    	}
    It gives me an error.
    what is the error your getting?

Similar Threads

  1. overriding toString method
    By matin1234 in forum New To Java
    Replies: 3
    Last Post: 06-01-2010, 04:35 AM
  2. toString() method error?
    By blueduiker in forum New To Java
    Replies: 6
    Last Post: 02-07-2010, 03:19 AM
  3. Trying to write toString method
    By curious in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 02:43 AM
  4. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 11:43 PM
  5. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 04:08 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
  •