Thursday 12 July 2018

Java: Check given string is valid java identifier or not

java.lang.Character class provides 'isJavaIdentifierStart' and 'isJavaIdentifierPart' methods to check whether given character can be part of valid variable name or not.

public static boolean isJavaIdentifierPart(char ch)
public static boolean isJavaIdentifierPart(int codePoint)
Return true, if the character may be part of a Java identifier, else false.

public static boolean isJavaIdentifierStart(char ch)
public static boolean isJavaIdentifierStart(int codePoint)
Return true, if the character may start a Java identifier, else false.


Below method checks whether given string is valid variable name or not.

         public static boolean isValidVariableName(String variableName) {
                  if (variableName == null || variableName.isEmpty()) {
                           throw new IllegalArgumentException("variableName can't be null (or) empty");
                  }

                  char[] charArray = variableName.toCharArray();

                  if (!Character.isJavaIdentifierStart(charArray[0])) {
                           System.out.println("variable name shouldn't start with " + charArray[0] + ", variable name : " + variableName);
                           return false;
                  }

                  for (int i = 1; i < charArray.length; i++) {
                           if (!Character.isJavaIdentifierPart(charArray[i])) {
                                    System.out.println("Variable name shouldn't contain " + charArray[i] + ", variable name : " + variableName);
                                    return false;
                           }
                  }
                  return true;
         }


Find the below working application.

Test.java

package com.sample.app;

public class Test {
 public static boolean isValidVariableName(String variableName) {
  if (variableName == null || variableName.isEmpty()) {
   throw new IllegalArgumentException("variableName can't be null (or) empty");
  }

  char[] charArray = variableName.toCharArray();

  if (!Character.isJavaIdentifierStart(charArray[0])) {
   System.out.println("variable name shouldn't start with " + charArray[0] + ", variable name : " + variableName);
   return false;
  }

  for (int i = 1; i < charArray.length; i++) {
   if (!Character.isJavaIdentifierPart(charArray[i])) {
    System.out.println("Variable name shouldn't contain " + charArray[i] + ", variable name : " + variableName);
    return false;
   }
  }
  return true;
 }

 public static void main(String args[]) {
  System.out.println("Is \"employee\" valid variable name? : " + isValidVariableName("employee"));
  System.out.println("Is \"@emp123\" valid variable name? : " + isValidVariableName("@emp123"));
  System.out.println("Is \"_emp\" valid variable name? : " + isValidVariableName("_emp"));
 }
}


Output

Is "employee" valid variable name? : true
variable name shouldn't start with @, variable name : @emp123
Is "@emp123" valid variable name? : false
Is "_emp" valid variable name? : true

You may like

No comments:

Post a Comment