aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Valid Identifiers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Valid Identifiers." Watch "Valid Identifiers." New topic
Author

Valid Identifiers.

Anthony Roy
Greenhorn

Joined: Oct 08, 2003
Posts: 8
I have seen quite a few threads on this topic. The Java Language Specification notes that Identifiers cannot match any keyword, true, fals or null. In addition, the following requirements are enforced:
Starting character: Must be a Java Letter.
Other Characters: Must be a Java Letter or a Java Digit.
The definitions of Java Letter and Java Digit are suitably vague! It seems that the only sure way of knowing is by using the Character.isJavaIdentifierStart(char c) and Character.isJavaIdentifierPart(char c) methods.
On my (UK) English system, this equates to JL: [a-zA-Z_$�]; JD: [0-9]
The following program may aid those with international keyboards:
import java.util.TreeSet;
public class JavaIdentifiers{
public static void main(String[] args){

String chars = (args.length > 0) ? args[0] :
"1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"+
"`��\\|!\"�$%^&*()-_=+[]{};:'@#~,.<>/?";
char[] charArray = chars.toCharArray();
TreeSet javaLetters = new TreeSet();
TreeSet javaDigits = new TreeSet();
TreeSet other = new TreeSet();

for (int i=0; i<charArray.length; i++){
char current = charArray[i];
if ( Character.isJavaIdentifierStart(current) ) {
javaLetters.add(new Character(current));
} else if ( Character.isJavaIdentifierPart(current) ){
javaDigits.add(new Character(current));
} else {
other.add(new Character(current));
}
}
System.out.println("Allowed in Identifiers:");
for (Iterator it = javaLetters.iterator(); it.hasNext(); ){
System.out.print(it.next().toString());
}
System.out.println("");

System.out.println("Allowed in Identifiers (but not as first character):");
for (Iterator it = javaDigits.iterator(); it.hasNext(); ){
System.out.print(it.next().toString());
}
System.out.println("");

System.out.println("Not Allowed in Identifiers:");
for (Iterator it = other.iterator(); it.hasNext(); ){
System.out.print(it.next().toString());
}
}
}


Ant...<br /><a href="http://www.antroy.co.uk" target="_blank" rel="nofollow">www.antroy.co.uk</a>
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Anthony, good stuff. You have to or to compile.
[ October 08, 2003: Message edited by: Vad Fogel ]
Anthony Roy
Greenhorn

Joined: Oct 08, 2003
Posts: 8
Whoops! Cut and paste error I'm afraid...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Valid Identifiers.