aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find the Java keyword " Watch "How to find the Java keyword " New topic
Author

How to find the Java keyword

Senthilrajan Chennai
Greenhorn

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
else
// 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.

Regards,
JD
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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 ) )


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find the Java keyword
 
Similar Threads
Problem: string concatenation
how can we find the exact size of java Objects like used to find in C++ by sizeof() ?
What is the best way to search for a string in a string?
Escape Character in String Parsing
retrieving value from a text box