Results 1 to 11 of 11
Like Tree2Likes
  • 2 Post By AvivC

Thread: toString() method question (Java newb)

  1. #1
    DFORMS is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default toString() method question (Java newb)

    Hey guys, I'm new to this forum and new to Java programming. I don't understand why this code is not outputting anything to the console?

    Java Code:
    public class Card {
    	
    	int cardValue;
    	String cardType;
    	
    	public void toString(int x, String y){
    		int value = x;
    		String type = y;
    		System.out.println(new Integer(value).toString() + " of " + type);
    	}
    	
    	public static void main(String [] args) {
    		Card newCard = new Card();
    		newCard.cardValue = 2;
    		newCard.cardType = "Hearts";
    		newCard.toString();
    	}
    	
    }
    I've looked around and I can't find an answer. I would appreciate if someone could help me out/point me in the right direction. Thank you!

  2. #2
    naik.akanksha is offline Member
    Join Date
    Jul 2014
    Posts
    1
    Rep Power
    0

    Default Re: toString() method question (Java newb)

    Hi,

    you have just written newCard.toString(); this does not call your method toString(int x, String y).

    if you update this to
    public static void main(String [] args) {
    test newCard = new test();
    newCard.cardValue = 2;
    newCard.cardType = "Hearts";
    System.out.println(newCard.toString());
    }
    then you can see some output :)

  3. #3
    DFORMS is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Re: toString() method question (Java newb)

    Hi Naik, thank you for the response.

    Java Code:
    public class Card {
    	
    	int cardValue;
    	String cardType;
    	
    	public String toString(){
    		return (new Integer(cardValue).toString() + " of " + cardType);
    	}
    	
    	public static void main(String [] args) {
    		Card newCard = new Card();
    		newCard.cardValue = 2;
    		newCard.cardType = "Hearts";
    		System.out.println(newCard.toString());
    	}
    }
    I was hoping to include the System.out.println(); in the toString method. But I'm not sure if that's what I actually have to do.
    Last edited by DFORMS; 07-15-2014 at 04:28 AM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: toString() method question (Java newb)

    Quote Originally Posted by DFORMS View Post

    I was hoping to include the System.out.println(); in the toString method. But I'm not sure if that's what I actually have to do.
    Code should do what its name indicates so you can make an educated guess by only reading its name what you can use it for. A 'toString()' method, with or without parameters, should not print anything; nor should it be used to set class attributes like in your original code. It should return the object as a String representation.

    You are thinking in the right direction though, I see what you want to achieve and its all good. You just need to keep studying a little more to learn the proper ways to do what you want to achieve :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    DFORMS is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Re: toString() method question (Java newb)

    Hey Gimbal, thanks for replying. I started learning Java two months ago and I am slowly getting there. Working full time and studying isn't always easy. The question I'm trying to answer I feel is poorly phrased and I'm having trouble understanding what exactly is required. Question translation (it's in french): ...The class Card also needs public void toString() method to print the value and type to the console in text. That's why I was looking to print from the method and use parameters in my first post. So I have to find out how to make it work with public void toString and not public String toString. I asked my online teacher that only answers my emails once every 6 or 7 days and he emailed me back saying that card.toString() would produce "2 of Hearts". Again having trouble understanding his instructions but I guess I can't use something like System.out.println(newCard.toString()); and need to print from the method...
    Last edited by DFORMS; 07-15-2014 at 10:15 PM.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: toString() method question (Java newb)

    Quote Originally Posted by DFORMS View Post
    The class Card also needs public void toString() method to print the value and type to the console in text. That's why I was looking to print from the method and use parameters in my first post. So I have to find out how to make it work with public void toString and not public String toString. I asked my online teacher that only answers my emails once every 6 or 7 days and he emailed me back saying that card.toString() would produce "2 of Hearts". Again having trouble understanding his instructions but I guess I can't use something like System.out.println(newCard.toString()); and need to print from the method...
    Not possible.
    The name will clash with Object#toString(), which returns a String.

    I can only assume that's a mistake on the part of your teacher, or whoever wrote the question.
    Write a proper toString method that returns the String you want, and call System.out.println(<your object>.toString())...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    DFORMS is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Re: toString() method question (Java newb)

    Thank you Tolls. I did a lot of researching before posting here and I couldn't find the solution... So I'm happy to know that it's not possible. I will submit this code and see what he says.

    Java Code:
    public class Card {
         
        int cardValue;
        String cardType;
         
        public String toString(){
            return (new Integer(cardValue).toString() + " of " + cardType);
        }
         
        public static void main(String [] args) {
            Card newCard = new Card();
            newCard.cardValue = 2;
            newCard.cardType = "Hearts";
            System.out.println(newCard.toString());
        }
    }

  8. #8
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Re: toString() method question (Java newb)

    Not sure if somebody already answered, but toString() and toString(int x, String y) are completely different methods, despite having the same name. That's why calling toString() doesn't invoke your method toString(int x, String y).
    Mshelly and gimbal2 like this.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: toString() method question (Java newb)

    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Re: toString() method question (Java newb)

    Why not this? Should work. Im new to Java.

    Java Code:
    public class Card {
         
        public void toString(int x, String y){
            int value = x;
            String type = y;
            System.out.println(new Integer(value).toString() + " of " + type);
        }
         
        public static void main(String [] args) {
            Card newCard = new Card();
            newCard.toString(2, "Hearts");
        }
         
    }
    ? That should do it. The toString method is completly different since you added arguements to it. toString(arg, arg)

  11. #11
    DFORMS is offline Member
    Join Date
    Jul 2014
    Posts
    9
    Rep Power
    0

    Default Re: toString() method question (Java newb)

    Yes, that works.

Similar Threads

  1. Basic Newb question
    By Dark Knight in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-07-2014, 09:31 AM
  2. Android Java NewB Question...
    By dmagician in forum Android
    Replies: 0
    Last Post: 06-25-2012, 11:32 AM
  3. A newb question
    By JavaJunkie2341 in forum New To Java
    Replies: 2
    Last Post: 03-30-2012, 07:00 PM
  4. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-12-2008, 11:13 PM
  5. toString() method of java.security.MessageDigest
    By Nicholas Jordan in forum Advanced Java
    Replies: 4
    Last Post: 09-18-2008, 02:32 PM

Posting Permissions

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