1

I didn't found any algorithms or regular expression or libraries in google for checking given String is a valid Java Identifier or not. I know the rules of valid declaration for java identifier (or may be help with IDE).

Can somebody support me How to check valid java identifer ?

PS : please don't be request to describe my efforded codes because I am not too familiar with regular expressions and I know I can write with hard-codes by myself. I am finding better and easy way to check it.

Cataclysm
  • 5,537
  • 12
  • 65
  • 114
  • 1
    First result on google : http://stackoverflow.com/questions/5205339/regular-expression-matching-fully-qualified-java-classes – Patrick Mar 20 '15 at 10:21
  • 2
    You can check here : [Check whether the given String is a valid identifier according to the Java Language specifications.](http://www.java2s.com/Code/Java/Reflection/CheckwhetherthegivenStringisavalididentifieraccordingtotheJavaLanguagespecifications.htm) – Not a bug Mar 20 '15 at 10:23
  • @Patrick OP wants to check if string can be used as reference name, not necessary as a type. – Pshemo Mar 20 '15 at 10:24
  • @Patrick do you mean rules of Identifiers are the same with naming classes ? – Cataclysm Mar 20 '15 at 10:24
  • 1
    @Cataclysm No these rules are different, for instance references can't contain `.` in it while `.` is valid part of type name since it describes its package structure. – Pshemo Mar 20 '15 at 10:26
  • 1
    BTW after validating if structure of your string is valid to be variable name you will need to check if it is not [Java reserved keyword](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html). – Pshemo Mar 20 '15 at 10:31
  • The link in your post referes to JPQL identifiers. What are you looking for `Java` or `Java Persistence Query Language` identifiers? – SubOptimal Mar 20 '15 at 10:35
  • @SubOptimal Yes , you right ! I am looking for `Java`. So , I am wrong , I think it is oracle official link for Java Identifier. – Cataclysm Mar 20 '15 at 10:39
  • If your are using Java 6+, consider `javax.lang.model.SourceVersion` class. It has methods for such stuff. – Amir Pashazadeh Sep 10 '17 at 13:37

1 Answers1

0

I think you should try this

public class IdentifierTest {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
    for(String arg : args) {
        boolean start = true;
        boolean validIdentifier = true;
        arg.toCharArray() 
        // commenter pointed out my error
        //for(byte b : arg.getBytes()) {
        for(char b : arg.toCharArray()) {
            if(start) {
                validIdentifier = validIdentifier && Character.isJavaIdentifierStart(b);
                start = false;
            } else {
                validIdentifier = validIdentifier && Character.isJavaIdentifierPart(b);
            }
        }
        System.out.println("Identifier \""
        + arg + "\" is "
        + (validIdentifier ? "" : "not ")
        + "valid");
    }
}

}

pamitha
  • 55
  • 1
  • 5