Hey Guys, please I need help badly. Thank You

I read a text file from my hard disk which contains as follows....

void main()
{
int A = 5;
int B = 5;
int C ; 
C = A + B;
cout << C ; 
}

So, what I need to do is that..
Lets say I have an array of...

String []KeyWord = {"void", "main()"};
String []DataType = {"int", "float"};

So I want to loop through each token and check whether for example its a key word or a datatype. I used java netBeans and I code as follows

int k = 0; int l = 0;

StringTokenizer Tokens;

while ((CurrentLine = ReadFile.readLine()) != null)
{
Tokens = new StringTokenizer(CurrentLine, " ", true);
for (int i = 0; Tokens.hasMoreTokens(); i++)
{
if (Tokens.nextToken().contains(KeyWord[k]))
{
jTextArea1.append(KeyWord[k] + "\n");
k++;


} 
else if (Tokens.nextToken().contains(DataType[l]))
{
jTextArea2.append(DataType[l] + "\n");

}

} 
} 

I RECEIVE ERRORS I DON'T UNDERSTAND. PLEASE HELP ME GUYS THANKS...

Please post the exact complete text of your errors

Comments
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)

That's not the complete message. Somewhere in the next few ines is the line number in your code where the error was detected.

... but I did notice you have a

9  for (int i = 0; Tokens.hasMoreTokens(); i++)

loop, but in that loop you read two tokens..

10     if (Tokens.nextToken().contains(KeyWord[k]))
17     else if (Tokens.nextToken().contains(DataType[l]))

so if there are an odd number of tokens the second nextToken will fail, probably the cause of your NoSuchElementException

This article has been dead for over six months. Start a new discussion instead.