• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exception Errors in Simple Program?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello newbie here, I'm trying to read lines from a textfile and count all the words using String Tokenizer. However I keep getting an error "Unhandled exception type FileNotFoundException" on my IDE(Eclipse) referring to lines 13 and 8. When I let the IDE automatically, and I've even tried typing this manually, insert a try-catch block more errors show up concerning inputFile. When I insert the throws FileNotFoundException for each method it compiles but after running it gives me a FileNotFoundException for textfile.txt. What's even more interesting is that when I copy the .java file out of the IDE project folder, place it in a random empty folder on the Desktop with the textfile.txt, and try running it through command line, it gives a NoSuchElementException: No line found.



Here is my textfile:
cat
dog
rat
fish
car,automobile,vehicle
up,down,left,right
arrow,zebra
one,two,three,4,5,six,77777
ten,twenty,thirty-three,forty;fift

 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting the try/catch statements in is the correct thing to do.
Where have you put your text file you are trying to read?

When you run the program in your IDE it isn't finding the text file.
My suggestion: Find out where it THINKS the file should be.



That will tell you exactly where it is looking for the file to open.

Two ways to fix it
- Move the file to where it is 'expected' to be
- Specify a directory for the file as well as a filename to tell it exactly where you want it (remember you will need to escape \ characters in a java string)
So it might be:


 
Oliver York
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Putting the try/catch statements in is the correct thing to do.
Where have you put your text file you are trying to read?

When you run the program in your IDE it isn't finding the text file.
My suggestion: Find out where it THINKS the file should be.



That will tell you exactly where it is looking for the file to open.

Two ways to fix it
- Move the file to where it is 'expected' to be
- Specify a directory for the file as well as a filename to tell it exactly where you want it (remember you will need to escape \ characters in a java string)
So it might be:




Hey Steven, thanks for the response. I replaced the throws exception with try-catch, and found the location of where the IDE looks and placed the text file there, and it seems like it can find it now but I get this error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at P2.numOfTokens(P2.java:24)
at P2.main(P2.java:7)

Here is my altered code:

 
Marshal
Posts: 80666
478
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That code is hard to read because of its inconsistent indentation. We have some suggestions here. I shall try improving it with the cheat's technique.And now we can read it

Have you been told to use tokenizer; it is legacy code and not recommended for use in new code? Do you want the semicolon as well as the comma in the delimiter on line 22? I would use String#split there:-
String[] words = line.split(",");
You can get the number of tokens from the length of that array.

The Exception you are getting is caused by there not being as many lines in the file as you thought. The problem is that you are using the BufferedReader way to read lines and the Scanner way to read lines is different:-What was happening was that you were continuing to read beyond the end of the file. The Scanner won't return null, as a BufferedReader does; it throws an Exception. You need to use the hasNextXXX methods.

There is a simpler way to count tokens with a Scanner.
 
Campbell Ritchie
Marshal
Posts: 80666
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Earlier, I wrote: . . . the cheat's technique. . . .

Which I learnt from one of the other mods.

Open a file in an IDE, e.g. Eclipse. Copy and past the entire class onto that file. Use ctrl‑A for select all and ctrl‑I for indentation. Copy and paste it all back. You can use ctrl‑F for formatting, too.
 
Oliver York
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch
Have you been told to use tokenizer; it is legacy code and not recommended for use in new code? Do you want the semicolon as well as the comma in the delimiter on line 22? I would use String#split there:-
String[] words = line.split(",");
You can get the number of tokens from the length of that array.

The Exception you are getting is caused by there not being as many lines in the file as you thought. The problem is that you are using the BufferedReader way to read lines and the Scanner way to read lines is different:-What was happening was that you were continuing to read beyond the end of the file. The Scanner won't return null, as a BufferedReader does; it throws an Exception. You need to use the hasNextXXX methods.

There is a simpler way to count tokens with a Scanner.



Hey I used the hasNext method for the while loop but it still giving me the same error.
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at P2.numOfTokens(P2.java:24)
at P2.main(P2.java:7)

Here is the code:
 
Campbell Ritchie
Marshal
Posts: 80666
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you are calling nextLine before you check with hasNextLine. That nextLine call should be inside the loop, and you should only call nextLine at the start of the loop.
 
Greenhorn
Posts: 1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Campbell Ritchie asking you to use inputFile.hasNextLine() as below:



Since the inputFile.nextLine() searches for text in the next line but the file came to its end you got NoSuchElementException
 
Campbell Ritchie
Marshal
Posts: 80666
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't try to put bold tags inside code tags; they don't work. I shall go back to your post and remove them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic