Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileNotFoundException on OOP-3 SortNames

 
Mark Channer
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Channer
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems everything is alright to me. Try running without the classpath.
 
Mark Channer
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Channer
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Mark Channer
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic