How do I make a custom exception to determine that a string != 1? So far this is what I have, but I'm not sure if it's anywhere close to being right.
I'm trying to program a card game, and I want to throw an exception for
if (rank.length() != 1) {
return;
}
So, I'd like to have an exception for that.
public class StringLengthException extends Exception{
public StringLengthException () {}
public StringLengthException (String message)
{
super(message);
}
}
Here's the class I'm trying to write the exception for
/**
* Name mutator.
*
* Business rules: - should be in the range A, 1, ..., 9, T, J, Q, K
*
* @param rank the rank to set
*/
public void setRank(String rank) {
// make sure the rank isn't null
if (rank == null) {
throw new NullPointerException ("Rank is null");
}
// make sure the rank isn't too long or too short
if (rank.length() != 1) {
return;
}
rank = rank.toUpperCase();
// check if the rank is one of the allowed ones
if ("A23456789TJQK".contains(rank)) {
this.rank = rank;
// is this an ace?
if (rank.equals(ACE)) {
this.value = 1;
} else // perhaps it is a face card?
if ((TEN + JACK + QUEEN + KING).contains(rank)) {
this.value = 10;
} else {
// it must be a regular card
this.value = Integer.parseInt(rank);
}
}
}