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 13 of 13

Thread: How to choose a different toString() Besides the default one?

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default How to choose a different toString() Besides the default one?

    Basically I have an object class

    public class Card 
    {
     
       private int value; // value of card (1-10, ...)
     
       // return String representation of Card
       public String toString() 
       { 
          return face + suit;
       } // end method toString
     
       public int getValue()
       {
    	   return value;
       }
    } // end class Card

    Sooo this Card object gets used multiple times and put into an array. But my problem is that I can't get figure out how to get to the Value of the card..
    I've been using this to print out the card i get from the array
    System.out.println(myDeckOfCards.getDeck()[ 0 ]);

    But, as you can see from the toString() Method up there it will only return the face and the suit. How do i get it to return the Value (on its own)? Or how can I access the Value for that particular element in the array?

    Thanks so much for any help.
    Last edited by Hallowed; April 8th, 2011 at 03:10 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to choose a different toString() Besides the default one?

    Recommended reading: Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    If its not obvious after reading that link, you don't choose another toString method, but override the existing one provided by the class Object.
    @Override
    public String toString(){
    ///return whatever you wish
    }

  3. The Following User Says Thank You to copeg For This Useful Post:

    Hallowed (April 6th, 2011)

  4. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to choose a different toString() Besides the default one?

    I am still kind of confused by this. I think I would need something like this:
    @Override
    public int toString(){
    return value;
    }
    But wouldn't I have to put that in the Card class? I still need to be able to access the other toString normally. and I'm confused about how to access or use the override one separately.
    Also from reading the links you sent me, could I some how use the hashCode() to get my value?
    public int hashCode(){
    	   return value;
       }
    ?
    if I normally get my toString like this:
     
    System.out.println(myDeckOfCards.getDeck()[ 0 ]);
    How could I use the hashCode? or the overridden toString. I'm confused about where I need to override it at, because it just tells me its wrong when I try to put it in the Card class with the other one.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to choose a different toString() Besides the default one?

    Write a method to return whatever you like and call that method instead of allowing the toString method to be called.

  6. The Following User Says Thank You to Junky For This Useful Post:

    Hallowed (April 8th, 2011)

  7. #5
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to choose a different toString() Besides the default one?

    I don't know how to call that method out of the array thats my problem. I already have the getValue in the card class, but I'm really new to arrays and can't figure it out.. I need some specific examples or code. This is what I have and it wont work right.
     
     
     
       // return String representation of Card
       public String toString() 
       { 
          return face + suit;
       } // end method toString
     
       public int hashCode(){
    	   return value;
       }
       public int getValue()
       {
    	   return value;
       }
    } // end class Card



     
     
    	public Card[] getValue()
    	{
    		return getValue();
    	}
    } // end class DeckOfCards

    it keeps giving me an error every time I try to get the value.

    Exception in thread "main" java.lang.StackOverflowError
    at DeckOfCards.getValue(DeckOfCards.java:80)
    Last edited by Hallowed; April 8th, 2011 at 03:09 PM.

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to choose a different toString() Besides the default one?

    Have you added any print lines to this, or better yet, stepped through it with a debugger, to see what's going on?

    Hint- What happens when getValue() is called?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #7
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to choose a different toString() Besides the default one?

    Yes, i tried printing it and it gives me Exception in thread "main" java.lang.StackOverflowError
    at DeckOfCards.getValue(DeckOfCards.java:80).
    I don't know what that means.
    As far as what happens, if I try to access the Card class the getValue method always returns the value of the last card that was put into the array 10.
    If I try to get the value from an object in the array using the getValue I have in DeckOfCards it just gives me that error and wont work. I've tried using
    DeckOfCards myDeckOfCards = new DeckOfCards();
    System.out.println(myDeckOfCards.getValue());
    to print it but it wont. I honestly have no idea how to call a method out of an array object. I don't think I'm accessing it right.

  10. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to choose a different toString() Besides the default one?

    The problem is your getValue() function calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which calls the getValue() function which...
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #9
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to choose a different toString() Besides the default one?

    Yea I know that is incorrect. I don't know how to access a method in an object that's been put into an array... which is what I'm trying to get help with.
    public Card[] getValue()
    	{
    		return deck.getValue;
    	}
    I tried that among other things, but i have little experience with arrays and objects in them and would appreciate some help with my actual question.

  12. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to choose a different toString() Besides the default one?

    Quote Originally Posted by Hallowed View Post
    I tried that among other things, but i have little experience with arrays and objects in them and would appreciate some help with my actual question.
    You haven't really asked a specific question. You've told us what you aren't familiar with, and you were given several suggestions. The latest Exception you posted was a StackOverFlowException, which is caused by your endless recursive method. I point that out, and you complain that we aren't helping you with your actual question. Seems pretty rude to me. Good luck, I'm out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Hallowed (April 8th, 2011)

  14. #11
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default

    How do I get the card value out of an object that's been put in the array? or how do i call a method from an object in an array.

    Is it even possible?
    Last edited by helloworld922; April 8th, 2011 at 12:12 AM.

  15. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to choose a different toString() Besides the default one?

    In the future, please use the edit feature to edit your posts.

    You can get objects out of an array using the indexing operators. Once you have that object, you can call the method you want.

    String[] lines = new String[]{"hello", "world", "java", "programming"};
    System.out.println(lines[1].toUpperCase()); // gets the string "world" and calls the member method toUpperCase() on that string

  16. The Following User Says Thank You to helloworld922 For This Useful Post:

    Hallowed (April 8th, 2011)

  17. #13
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How to choose a different toString() Besides the default one?

    Thank you so much! That is exactly what I needed.

    public static int ArrayPosition = 0;
     
    public int getValue()
    	{
     
    		return getDeck()[ArrayPosition].getValue();
     
    	}
    Last edited by Hallowed; April 8th, 2011 at 03:14 PM.

Similar Threads

  1. Help me to Choose Collections
    By kathir0301 in forum Collections and Generics
    Replies: 1
    Last Post: December 3rd, 2010, 10:14 AM
  2. How do I let the user choose how big an array is...
    By Roon Hapoon in forum Collections and Generics
    Replies: 2
    Last Post: October 5th, 2010, 12:15 AM
  3. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM
  4. why should i choose java over others?
    By docesam in forum Java Theory & Questions
    Replies: 9
    Last Post: August 30th, 2009, 10:49 PM
  5. Replies: 0
    Last Post: May 21st, 2009, 11:17 AM