문자열을 확인하기 위해 사용자 정의 예외를 만들려면 어떻게해야합니까? = 1? 지금까지 이것이 내가 가지고있는 것이지만, 그것이 옳다는 것이 거의 확실한 것인지 잘 모르겠습니다. 그래서, 그에 대한 예외를 가지고 싶습니다 자바에서 문자열에 대한 사용자 정의 예외
나는 카드 게임을 프로그래밍하기 위해 노력하고있어, 나는if (rank.length() != 1) {
return;
}
에 대한 예외를 throw합니다. 여기
public class StringLengthException extends Exception{
public StringLengthException() {}
public StringLengthException (String message)
{
super(message);
}
}
내가 문자열의 길이가 1에 동일한 경우는, 예외를 던져 그렇다면,
/**
* 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);
}
}
}
당신은'IllegalArgumentException'을 찾고 계십니까? –
위의 편집을 확인하십시오. – user3382217