【发布时间】:2025-12-28 19:10:06
【问题描述】:
所以我一直收到一个错误,说它在“if (Character.isValidHex(thisChar, 16)) {”行找不到符号,我想知道这个方法的格式是否正确?在确定每个字符是否为数字后,我很难确定每个字符是否为有效的十六进制值(a-f)。这是确定有效性的部分,这是我唯一的错误。我必须将使用 isdigit 的部分和确定它是否为十六进制值的部分分开,我不能组合和使用 Character.digit(thisChar, 16) !!!请帮忙!!
public static boolean isValidHex(String userIn) {
boolean isValid = false;
// The length is correct, now check that all characters are legal hexadecimal digits
for (int i = 0; i < 4; i++) {
char thisChar = userIn.charAt(i);
// Is the character a decimal digit (0..9)? If so, advance to the next character
if (Character.isDigit(thisChar)) {
isValid = true;
}
else {
//Character is not a decimal digit (0..9), is it a valid hexadecimal digit (A..F)?
if (Character.isValidHex(thisChar, 16)) {
isValid = true;
}
else {
// Found an invalid digit, no need to check other digits, exit this loop
isValid = false;
break;
}
}
}
// Returns true if the string is a valid hexadecimal string, false otherwise
return isValid;
}
【问题讨论】:
-
当您说
if (Character.isValidHex(thisChar, 16)) {时,您的班级是否命名为Character?如果是这样,您将需要使用完全限定的类名。因为java.lang.Character会影响你自己的班级。 -
你看过Character API了吗?这是您应该从这些问题开始的地方。