How to choose a different toString() Besides the default one?
Basically I have an object class
Code :
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
Code :
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.
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.
Code :
@Override
public String toString(){
///return whatever you wish
}
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:
Code :
@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?
Code :
public int hashCode(){
return value;
}
?
if I normally get my toString like this:
Code :
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.
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.
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.
Code :
// 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
Code :
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)
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?
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
Code :
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.
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...
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.
Code :
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.
Re: How to choose a different toString() Besides the default one?
Quote:
Originally Posted by
Hallowed
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.
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.
Code Java:
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
Re: How to choose a different toString() Besides the default one?
Thank you so much! That is exactly what I needed.
Code :
public static int ArrayPosition = 0;
public int getValue()
{
return getDeck()[ArrayPosition].getValue();
}