• 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:

Issue with reading from a file

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it. You invoke readLine() twice. And with every incocation, you advance by one line.
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of a String return the List we have generated.

 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
alright sounds like a tall task. I will give it a shot tomorrow. Thanks a lot for your patience with me.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do we need to write fr.close(); ?

I guess br.close(); is sufficient. Correct me if I'm wrong.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right. The close method is propagated up the chain.
 
reply
    Bookmark Topic Watch Topic
  • New Topic