This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 12759
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 ) )
subject: How to find the Java keyword
Similar Threads
Escape Character in String Parsing
retrieving value from a text box
Problem: string concatenation
What is the best way to search for a string in a string?
how can we find the exact size of java Objects like used to find in C++ by sizeof() ?