• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Valid Identifiers.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
}
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anthony, good stuff. You have to or to compile.
[ October 08, 2003: Message edited by: Vad Fogel ]
 
Anthony Roy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops! Cut and paste error I'm afraid...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic