0

I am trying to make a method that will validate a type of ID called gNum.

The gNum must be made up of 9 characters (first character being "G" and the rest being digits)

private boolean validateGNum (String gNum) {
      boolean valid = false;
      if(gNum.length() == 9) {

         /*this is where I have a problem. 
         Trying to see how I can check if the first character of the
         string 'gNum' is "G"
        */
         if(gNum.charAt(0).equalsIgnoreCase("g")) {
            for (int i=1; i < 9; i++) {
               if(Character.isDigit(gNum.charAt(i))) {
                  valid = true;
               }
            }   
         }
      }  
       return valid; 
   }

This is what I have and I get 'char cannot get dereferenced' error.

How can I fix this issue and make this code work?

Thank you

veritas
  • 11

4 Answers4

1

What you're looking for is the substring method:

if(gNum.substring(0, 1).equalsIgnoreCase("g")){...}

As an aside you can improve the loop by returning false if you find any character that is not a valid digit. Currently, you will return true if any character after "g" is a valid digit which is incorrect;

example:

boolean validateGNum (String gNum) {
      if (gNum.length() == 9) {    
          if (gNum.substring(0, 1).equalsIgnoreCase("g")){
              for (int i = 1; i < 9; i++) {
                 if (!Character.isDigit(gNum.charAt(i))) return false; 
              }
              return true;  
          }                             
      }
      return false;
}

or if you want to get a bit fancy then you could do:

static boolean validateGNum (String gNum) {
        return gNum.length() == 9 
                && gNum.substring(0, 1).equalsIgnoreCase("g") 
                && gNum.substring(1)
                       .codePoints()
                       .allMatch(Character::isDigit);
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

Your condition should change to

if(Character.toUpperCase(gNum.charAt(0)) == 'G')

You cannot call a method on a primitive type, not even to mention that you cannot invoke a String method on a char.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

Just use == to compare the characters. And there's another mistake, it will return true with just one digit, not when all are digits.

private static boolean validateGNum(String gNum) {
    boolean valid = false;

    if (gNum.length() == 9)
        if (gNum.charAt(0) == 'G')
            for (int i = 1; i < 9; i++)
                if (!Character.isDigit(gNum.charAt(i)))
                    return false;
                return true;

    return valid;
}
Lionel
  • 5
  • 3
0

A regular expression Pattern is an excellent class to use for this kind of task. In the most basic form the String class already offers a matches method. If you need to compare multiple strings creating a separate Pattern and new Matchers might be more efficient as it avoids reinterpretation of the pattern.

The pattern to use is "[gG]\\d{8}", meaning a lowercase or capital G, followed by exactly 8 digits.

jshell> System.out.println("G12345678".matches("[gG]\\d{8}"));
true

jshell> System.out.println("g12345678".matches("[gG]\\d{8}"));
true

jshell> System.out.println("G1234567".matches("[gG]\\d{8}"));
false

jshell> System.out.println("G123456789".matches("[gG]\\d{8}"));
false

jshell> System.out.println("Z12345678".matches("[gG]\\d{8}"));
false
M. le Rutte
  • 3,525
  • 3
  • 18
  • 31