aspose file tools*
The moose likes Beginning Java and the fly likes no such element? eek! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "no such element? eek!" Watch "no such element? eek!" New topic
Author

no such element? eek!

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
This program is supposed to take a text file, and search for a certain string entered by the user, and come up with the strings line location and column location. When I run the program I keep getting this error at the command promt. I know for a fact the string I was searching for is in the text file. Any advice?
Error:
Please enter the file's location:
c:\java\caffeine.txt
Please enter the string you would like to locate:
coffee
c:\java\caffeine.txt coffee
There were matches at:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:232)
at StringMatch.main(StringMatch.java:58)
Press any key to continue . . .
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
If there are no more tokens in the StringTokenizer object, nextToken() returns a NoSuchElementException. You need to check if there are any more tokens by using boolean hasMoreTokens().
Angel
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
how would I do that?
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
also there is extra reader.readLine() in your code, this cause first line being skipped without process.

fix this & fine-tune the CharCount, you are on the right track.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
how would I do that?

something like
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
okay so I changed it so that the reader is only initialized once and I also used hasMoreTokens (below is an example of my new while statements). Now when I run the program I get this output:
Please enter the file's location:
c:\BrandiJava\caffeine.txt
Please enter the string you would like to locate:
coffee
c:\BrandiJava\caffeine.txt coffee
There were matches at:
and then the program just stops running and I get no display of where the matches are. Any ideas as to why?

[ November 19, 2003: Message edited by: Brandi Love ]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Does the program finish normally without display any
matches or it just hang ?
For first case, check the case of your search input, for 2nd case,
could be something with readLine.

[/qb]<hr></blockquote>
[ November 19, 2003: Message edited by: chi Lin ]
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
the program just hangs, and doesn't do anything after that point. It doesn't finish.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
try change your outer while loop condition to

and comment all other reader.readLine() inside your code.
if still stuck, please post your current code.
[ November 19, 2003: Message edited by: chi Lin ]
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
That did the trick Thank ya all very much.
MEHULecs DIKSHITecs
Greenhorn

Joined: Nov 20, 2003
Posts: 4
//FileReader fr =new FileReader(fileName);
//BufferedReader br2 = new BufferedReader(fr);
System.out.println("Please enter the string you would like to locate:");
input = br.readLine();
System.out.println(fileName + " " + input);
total = 0;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String readLine = reader.readLine();
System.out.println("There were matches at:");

while ( readLine != null )
{
System.out.println(readLine);
st = new StringTokenizer(readLine, input);
//next = st.nextToken();
//charCount = charCount + next.length() + 1;
//lineCount = lineCount + 1;
while ( st.haToken() )
{
total++; // if coffe is is available then count++
//if (next.equals(input))
//{
//System.out.print("Line:" + lineCount + "\nColumn:" + charCount);
//}
}
readLine = reader.readLine();
}
System.out.println(total);
 
jQuery in Action, 2nd edition
 
subject: no such element? eek!