This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes TextFileIn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TextFileIn" Watch "TextFileIn" New topic
Author

TextFileIn

Armando Moncada
Ranch Hand

Joined: Nov 14, 2011
Posts: 30

I am on OOP Sort Names. My program cannot find file.txt . I read a post that said "ensure you include common.jar in your classpath and then import com.javaranch.common.TextFileIn"
I have done the import. So where is common.jar? is it part of jr.jar?


Armando
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
If you're asking how to obtain the common.jar, you can download it from the link provided on this page. If you meant something else, please clarify. I would expect you'd be getting a different error message if you didn't have the common.jar package but had an import statement to import it.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Armando Moncada
Ranch Hand

Joined: Nov 14, 2011
Posts: 30

My error message is FileNotFoundException: file.txt <The system cannot find the file specified>

I have done the download for common but I can't find TextFileIn or file.txt anywhere on my C drive.

Am I supposed to create a .txt file and put some names in there?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Not sure about TextFileIn implementation, but try giving the absolute path of the "file.txt" (like "C:/test/file.txt").

Am I supposed to create a .txt file and put some names in there?
Seems to me a yes.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Greg Brannon wrote:If you're asking how to obtain the common.jar, you can download it from the link provided on this page.

That page doesn't have a link to download common.jar. But it does have jr.jar.

Yes, Armando. TextFileIn is actually part of jr.jar. The other file is was from an older version of the code; the instructions were apparently never updated. You need to include jr.jar in your classpath, not common.jar. Carefully study the instructions you have for adding things to the classpath, and ask more here if you can't get it to work.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Although the error you're getting now suggests you already have jr.jar as part of the classpath, in which case you're fine. John Jai's advice is most likely what you need now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
Why is your tutorial telling you to use the TextFileIn class rather than the standard JavaSE API. There is a quicker way to terminate the loop, but it requires a strange idiom: while ((line = input.readLine()) != null)...
The reason for the extra pair of () is that the = operator has a lower precedence than the != operator, so you need the () to make sure the text is put into line before the null test. The () after try is the try with resources idiom, which only works in Java7.. . . where foo() is any method which takes a String and can do something with the line. Note you have 3 classes and 2 exceptions from the java.io package to import.

Another version to follow soon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
This version requires you to import the javax.swing.JFileChooser class, too, and you can find out more about it here.The close() call goes in a finally block to make certain that the reader is closed.
The showOpenDialog() method opens the GUI which allows you to navigate around and find the file. If you click the OK button, the method then completes and returns APPROVE_OPTION; if you click another button it returns something different. I have omitted the catch (FileNotFoundException fnf) ... because you won’t click OK until you have found the correct file.

By the way, your file chooser might sort out all the problems you are having about not being able to find that file.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Campbell Ritchie wrote:Why is your tutorial telling you to use the TextFileIn class rather than the standard JavaSE API.

I suspect the tutorial author had a heavy case of Not Invented Here syndrome. Though he would probably argue that much of the JavaSE API didn't exist back when this was written. And I think there's partly a point of getting people to learn to add a jar to the classpath and use third-party tools, which is a useful thing to know.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
Mike Simmons wrote: . . . I suspect the tutorial author had a heavy case of Not Invented Here syndrome.
That sounds a very good explanation.
Though he would probably argue that much of the JavaSE API didn't exist back when this was written.
BufferedReader and FileReader have “since 1.1” in their documentation, and File is “since 1.0”, so that must be a very old tutorial
And I think there's partly a point of getting people to learn to add a jar to the classpath and use third-party tools, which is a useful thing to know.
Agree it is useful to know how to import a .jar, but I think, in this case, the combination of imports and National Institutes of Health (or whatever NIH means) is jarring.

We have the OP confused about whether he is importing the .jar correctly, and simultaneously not finding the text file. There is such a thing as information overload, and I think we have an example here.
The while ((line = ...) != null)... idiom is something you have to show the newbies because they would never work it out in a month of Sundays otherwise.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Campbell Ritchie wrote:
Though he would probably argue that much of the JavaSE API didn't exist back when this was written.
BufferedReader and FileReader have “since 1.1” in their documentation, and File is “since 1.0”, so that must be a very old tutorial

And TextFileIn extends BufferedReader. It doesn't add anything except a constructor that accepts a filename, to combine a BufferedReader with a FileReader in one line, as a minor convenience. Nonetheless, I've heard him make that argument in the past. Go figure.
Armando Moncada
Ranch Hand

Joined: Nov 14, 2011
Posts: 30

I resolved it by simply creating a .txt file with some names in it and putting it in the classpath. I was under the mistaken impression that the .txt file came with the jar.

Nowhere in the instructions for this assignment (SortNames) does it say to do that.

Somebody needs to rewrite the assignment because us newbies take things literally.

That said, thanks to all of you who responded.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It looks to me like the example expected you to put the file in your current working directory. And it looks like you did that (most likely the current working directory is part of your classpath).

It's true that newbies can't be expected to know a lot of things. But learning what the current working directory is, that used to take place in the first hour of learning how to use the command line. (It isn't a Java concept, it's an operating system concept.) Unfortunately most newbies don't even get that much training in the command line these days, which puts even more burden on the unfortunate Java tutorial writers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: TextFileIn
 
Similar Threads
if file exists, then send email with attatchment
SortNames FileNotFoundException???
help needed with file reading in program.
Sending mail with body and attachment
negative booleaan again