This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

no such element? eek!  RSS feed

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 . . .
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would I do that?
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how would I do that?


something like
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the program just hangs, and doesn't do anything after that point. It doesn't finish.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did the trick Thank ya all very much.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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);
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!