Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ToString

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ToString

    Can anyone help solve my problem? Don't know where I've gone wrong.

    Main
    // Main
    public class Nicky {	
    	public static void main(String[] args)
    	{
    		func f = new func("Did it work? Hehehe", 10);
     
    		System.out.print(f());
    	}
    }

    Other class
    // functions
    public class func {
     
    	private String TheWord;
    	private final int TheNumber;
     
    	public func(String s, final int f)
    	{
    		TheWord = s;
    		TheNumber = f;
    	}
     
    	public String toString()
    	{
    		return String.format("The word is %s\nThe number is %d", TheWord, TheNumber);
    	}
    }

    And thanks for the help on the other post, should I report solve on there?

    - Nicky

    EDIT:

    Oops, I apologize, here's the error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method f() is undefined for the type Nicky

    at Nicky.main(Nicky.java:7)
    Last edited by Masic1990; August 25th, 2014 at 12:37 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: ToString

    The compiler doesn't know what 'f()' is. I believe you meant to cause the object 'f' to print by way of its toString() method, in which case you'd simply print 'f', not 'f()'.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ToString

    Ha, I guess I didn't see that. I'll refrain from making useless post before looking more next time, sorry.

    - Nicky

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: ToString

    Quote Originally Posted by Masic1990 View Post
    Ha, I guess I didn't see that. I'll refrain from making useless post before looking more next time, sorry.

    - Nicky
    There's no such thing as a "useless post" in theory. Everyone get's a programmers block now and then.
    Of course, everyone else wants to help you - which makes being a part of the forum even more important.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (August 25th, 2014)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: ToString

    Echoing Ada's comment, and hoping I didn't give you the impression there was anything useless about your post, a second set of eyes often see a simple error that we're unable to see ourselves for many reasons. While you're encouraged to figure things out on your own, don't hesitate to ask when you need help.

Similar Threads

  1. [SOLVED] Information on toString()
    By jamesh in forum Java Theory & Questions
    Replies: 5
    Last Post: January 14th, 2014, 07:15 AM
  2. Can I use toString() here?
    By titowinky in forum Java Theory & Questions
    Replies: 2
    Last Post: May 30th, 2012, 07:15 AM
  3. toString method
    By pelane in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 01:42 AM
  4. Can someone help me understand toString()?
    By Psychotron in forum Java Theory & Questions
    Replies: 10
    Last Post: January 18th, 2012, 09:43 PM
  5. Why cant I use toString() with vector?
    By tarkal in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 07:24 AM