• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find the Java keyword

 
Senthilrajan Chennai
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic