my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Issue with reading from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Issue with reading from a file" Watch "Issue with reading from a file" New topic
Author

Issue with reading from a file

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I am trying to create a phonebook program and I am stuck at reading from the file.

I have the following method

As you can see this reads from a file called phones.txt. This method is in working order, the only issue is that it returns null for data. If I change the name of the file to abc.txt for example, I get the FileNotFound exception so I think I am on the right track.

I am trying to print the file by using this line


Any ideas as to why I get null for my data? I would think it would print a list of the records in the phone text.

For reference my txt file looks like this:
Smith;John;332-5467
Jones;Mary;456-2312
Sam;Henry;345-0987



Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
your Read() function returns null for data because your while loop runs until data equals null ;-)

If you want to read the contents of the file into a String, use this piece of code:



JDBCSupport - An easy to use, light-weight JDBC framework -
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Why is your code better than mine? It seems more complex to do the same job. I guess I am looking for more of an explanation as to why mine doesnt work and how I might be able to fix it.

I changed data!=null to data == null and it returns the first line. This makes no sense to me.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
readLine() always returns the current line of the file.

When you instantiate the BufferedReader the pointer is at line 1.

Once you invoke readLine(), the BufferedReader calles the underlying read() method from the FileReader until it reaches it's endpoint (which is the new line character).
This is what readLine() returns to your data variable.

If you invoke readLine() again, it starts from the position where it left off, hence reads the next line.

As you, with every loop cycle, reassign the data variable, you will always end up with the last line of your text file being returned in data.

This is why I have created a StringBuilder and append each line to it, rather than overwriting the old value.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
So you are able to check whether line is not null and set line to the readLine() in the same while statement?

Also why did you make String line = "";

It works fine as String line.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Yes you can do everything in one line. The assignment (which has to be in parantheses) is evaluated and the result checked against null.

As for the String line ="";
You can leave it just as String s;

I made it a habit always initializing variables to something to prevent 'variable might not have been initialized' errors.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok one last thing


I would think this would work fine, but it only returns two results and a null value. Very strange...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Think about it. You invoke readLine() twice. And with every incocation, you advance by one line.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Well in my mind the while loop should not perform an action besides flow control. I guess you learn something new every day, I will have to remember that you can perform tasks from within the while loop.

One more thing, I am planning to read my data into an array of objects. Is there a good way to use the ; to do that?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Do you mean read each line into an array?

I would suggest using a List for that.

The implementation is easy. In spite of a StringBuilder, create an ArrayList<String> and with each loop cycle, you add the line to the list.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Well actually I would want to set it up so that my object has a firstname, lastname, and phone and create an array of those objects. The thing that is holding me up is how to delimit it with our new method.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok I have never used an ArrayList before but it was recommended to me before so I am going to give it a shot. I think it would be really helpful if you commented out the code starting with the array list stuff so I know what is happening.



Also I am thinking that I wont need getters b/c I am using the contents of the file to create my array list.

I think the array list should be nice thing to learn.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Also I am thinking that I wont need getters and setters b/c I am using the contents of the file to create my array list.


This is not what Getters and Setters are about.

Getters and Setters are to encapsulate and control instance variables. They also follow the JavaBean convention.
Directly accessing instance variables from other classes is considered bad style.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok so I need to somehow get the lastname, firstname and phone from my read class. The problem is I can only return one value, and I am not sure how to use the delimiter to separate the values. Any ideas?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Instead of a String return the List we have generated.

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
alright sounds like a tall task. I will give it a shot tomorrow. Thanks a lot for your patience with me.
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Do we need to write fr.close(); ?

I guess br.close(); is sufficient. Correct me if I'm wrong.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You're right. The close method is propagated up the chain.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with reading from a file
 
Similar Threads
Incompatible type issue
FILE I/O
Getting the MAC address and using that for licensing
Reading a text file using FileReader & BufferedReader : Unexpected output !
BufferedReader contents into Array