File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to find the Java keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find the Java keyword " Watch "How to find the Java keyword " New topic

How to find the Java keyword

Senthilrajan Chennai

Joined: Jan 28, 2007
Posts: 5
Hi Frnds,

Is there any way to find the given string as a java keyword ?

Actually I need to check the string like the following code..

String str = argv[0];
if(str is a javakeyword)
// do something
// do something

Instead of storing all the keywords in an array & then check , is there any other simple way to do it?
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
I don't believe that there are any magical java constructs that will tell you if a string is a java keyword. As a quick solution, create a class and method called for example - JavaUtils and isJavaKeyword (that returns a boolean) and have the method do exactly as you originally suggested (use an array - or some other type of collection). Perhaps you can load the keywords into the collection from a text file.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Instead of storing all the keywords in an array

Actually thats a job for a java.util.HashSet - it would give you the fastest possible boolean result. You would have to construct the HashSet first of course.

if( kwHashSet.contains( str ) )
I agree. Here's the link:
subject: How to find the Java keyword
It's not a secret anymore!