Two Laptop Bag*
The moose likes Cattle Drive and the fly likes FileNotFoundException on OOP-3 SortNames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "FileNotFoundException on OOP-3 SortNames" Watch "FileNotFoundException on OOP-3 SortNames" New topic
Author

FileNotFoundException on OOP-3 SortNames

Mike Carroll
Greenhorn

Joined: May 09, 2013
Posts: 17
Hi all,

I've just started having a go at the SortNames assignment and I seem to be stumbling at the first hurdle. I'm trying to set up the TextFileIn class to read the names that I've saved in names.txt, but when I run the code below that's given in the com.javaranch.common package I get a filenotfoundexception.


I've tried the suggestions in this thread, such as specifying the full path name like:
TextFileIn in = new TextFileIn( "C:\\JavaExercises\\names.txt" ) ; but I still get the filenotfoundexception.
This is most probably a daft question, but can I save a txt file using anything? I basically opened up notepad and copied in that list of eight names. I've made sure that it's saved in my JavaExercises folder, where my jr.jar file is.

Help!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2233
    
    7

You should put the txt file in the working directory.

Suppose your directory structure is C:\SortNames and your project SortNames.jar file is in C:\SortNames\dist then your txt should be in C:\SortNames. Then when you run the jar from C:\SortNames, (eg java -jar dist/SortNames.jar), the txt can be found. You may also need to specify the classpath for the javaranch package jar.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Mike Carroll
Greenhorn

Joined: May 09, 2013
Posts: 17
Thanks for your reply K,

Yes my txt file is in the working directory, which is basically a file called JavaExercises located straight off my C-drive, ie: C:\JavaExercises. My other classes, java files, as well as the java ranch jr file are located there also. I'm sure these are all working and set-up fine though, because I can still use other classes such a JDate and GDate from the java ranch file.

Just to confirm, my txt file is called names.txt and is basically a notepad document with a list of 8 names on it, nothing else. That's all I need, right?

My classpath is all set up fine I think. As mentioned above I can use other classes in the java ranch file without any issues. My classpath is: C:\JavaExercises;C:\JavaExercises\jr.jar (jr.jar is the Java Ranch file)

I don't get what I'm missing here, and it's driving me bat-sh*t crazy!!



K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2233
    
    7

Seems everything is alright to me. Try running without the classpath.
Mike Carroll
Greenhorn

Joined: May 09, 2013
Posts: 17
Just tried that, but still the same exception.
I've tried reinstalling the jr.jar file also, but that doesn't do the trick. I've also tried putting this stupid txt file in different folders but for some reason it's not getting recognized.
Gggrrrrrr.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2233
    
    7

I think we should step back a bit and evaluate the situation.

1. What is your classpath look like?
2. List out the files in your working directory using the dir command
3. Can you compile the program? What command did you use?
4. What command you use to run the program?
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 317
    
    5

Mike is the code you posted everything in your class file?

If it is then you are missing a very important package at the top of the file.
Mike Carroll
Greenhorn

Joined: May 09, 2013
Posts: 17
That sounds hopeful Margaret, what is it?

Thanks for your persistence K, here's the details:
1. Classpath: C:\JavaExercises;C:\JavaExercises\jr.jar
Path: C:\Program Files\Java\jdk1.7.0_21\bin

2. To get to my working directory, I type 'cd\JavaExercises'. Then if I type 'dir' from there the files I get are:
DaysOld.class
DaysOld.java
GeekWatch.class
GeekWatch.java
Grains.class
Grains.java
jr.jar
Leap.class
Leap.java
names.txt
SortNames.java

As you can see, SortNames.java, jr.jar, and names.txt are all in here.

3. I cannot compile the program. To compile, I first type 'cd\JavaExercises' and then 'javac SortNames.java'. This is when I get the message:
'unreported exception FileNotFoundException; must be caught or declared to be thrown'

4. Unfortunately I've not made it past compiling yet.

Thanks both of you! I really appreciate the help.


Mike Carroll
Greenhorn

Joined: May 09, 2013
Posts: 17
Finally got it sorted!

There were 2 problems:

1. I needed to import java.io.*; (thanks for the heads up Margaret)
2. I needed to add 'throws Exception' to the end of my main() method.

Thanks for your help guys!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2233
    
    7

Glad you sorted it out. I always thought you were able to compile and unable to find to the names.txt file.

Now you can actually work on the sorting of the names
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileNotFoundException on OOP-3 SortNames
 
Similar Threads
SortNames..
SortNames FileNotFoundException???
SortNames
OOP3 - SortNames is OK forwards, but can't reverse!
SortNames