-2

So I keep getting an error that says it cannot find symbol at the line "if (Character.isValidHex(thisChar, 16)) {" and I am wondering if this method is formatted correctly? I am having difficulty determining whether each individual character is a valid hex value (a-f) after determining if it is a digit or not. This is the section that determines the validity and this is my only error. I must separate the sections that use isdigit and the section that determines if it is a hex value, I cant combine and use Character.digit(thisChar, 16) !!! Please help!!


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;
}
Scary Wombat
  • 43,525
  • 5
  • 33
  • 63
  • 1
    Is your class named `Character` when you say `if (Character.isValidHex(thisChar, 16)) {`? If so, you'll need to use the fully qualified class-name. Since `java.lang.Character` is going to shadow your own class. – Elliott Frisch Oct 02 '18 at 02:16
  • Have you looked at the [Character API](https://docs.oracle.com/javase/10/docs/api/java/lang/Character.html) yet? This is where you should start for questions like these. – Hovercraft Full Of Eels Oct 02 '18 at 02:18

1 Answers1

-1

You can use Character.digit(thisChar, 16), since you have already filtered out 0 to 9 with Character.isDigit(thisChar).

From Javadoc of Character.digit():

If the radix is not in the range MIN_RADIX ≤ radix ≤ MAX_RADIX or if the value of ch is not a valid digit in the specified radix, -1 is returned.

if (Character.digit(thisChar, 16) != -1) {
    isValid = true;
}
Jai
  • 8,017
  • 2
  • 19
  • 47