• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble appending to a text file in Java?  RSS feed

 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am at a loss when it comes to appending Strings to a text file in Java. I was tasked (yes, homework) to complete a program that does the following simple things:
Print out the contents of a text file to the user (got that!)
Ask the user if the want to add any customers to this text file (got it!)
Add those customer's name's, addresses, postal codes and cities. (got that too)
Verify the postal code is in the proper format (yep!)
Add the new information to the text file, and display it to the user (Nope...)

The program is, essentially, supposed to keep track of the user's customers, and store this information to a text file. However, when I run the following code, I get a number of errors:



The errors I get are:

Java.io.exception. Main. Stream closed
ensureOpen (115)
ava.io.BufferedReader.readLine(BufferedReader.java:310)

I understand that the numbers correspond to lines of code (line 115, or 310, for example) but I am unsure of how to fix these errors. Any help/hints are greatly appreciated. Also, I will gladly except any constructive criticism that I can use to make my code easier to read, understand, etc. Thank you!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aarden Axford wrote:
Java.io.exception. Main. Stream closed
ensureOpen (115)
ava.io.BufferedReader.readLine(BufferedReader.java:310)


The appendInformation() method closes the fileRead buffered reader upon completion. And the main() method calls the appendInformation() method, many times in a loop, passing the same buffered reader. Obviously, this can only work for one iteration.

Henry
 
Aarden Axford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code itself seems to be working, however it seems to not be appending to the file. The file still has no new text.. why is this occurring? Any tips appreciated...

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aarden,
Aarden Axford wrote:The code itself seems to be working, however it seems to not be appending to the file. The file still has no new text.. why is this occurring? Any tips appreciated...
It seems it is not the biggest your problem at the moment.
What I see the main problem - is code readability, because is as hell difficult to read it and understand the program flow. I think that's why you're not able to understand where the program flow goes wrong and debug yourself in order to make it work properly.
Actually you even can't figure it out now, which part of the code doesn't perform as you expect.

Beside that, there are other more serious problems as all fields seems to be appeared as static in your program (I see you tried to play in the easiest way).

Hopefully the guys will step in and will help you to refactor your code, as I wouldn't have myself an idea where to start.
Probably I'd start over.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition (please don't get upset, I'm just trying to help),

Such a method names as "CustomerNumber" are very misleading for some reasons:
1. Capital letter normally starts Class names, Constructors.
2. If your method returns customer number, it should be named as a "verb". Something like - getCustomerNumber.

Variable names are also misleading:
1. "int customer = 0" ? maybe would be something more understandable as "customerNumber".
in your case, loop expression "for (int i = 0; i < customer; i++)" simply doesn't make sense for you and your code readers.
2. firstOutput - it is very difficult to understand also. Logical flow suggests that I suppose to find somewhere else secondOutput, thirdOutput, but it didn't manage to find it.
Hopefully you got the idea

What according to you this snippet of code meant to do?

Beside that, your comments also adds more confusion to it. I really think you should take some rest and start over.
Probably would be a great idea to get pencil and paper and write down the flow of the program in simple english, what has to be done after what, what program in general should achieve at each step.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you get full understanding about the problem (written in english or your native language on piece of paper), then you can start thinking about:
How to create simple structure, which should help easier track program flow.
Think about method names (descriptive), so you would know from the method name what it suppose to do.
Pick very accurate variable names (actually, if you look at variable name, and you need to look at further lines to have full understanding what it stores, it suggests, that variable name not good enough).

After you think about everything in details, you could try to follow simpler structure, than you have.
Could be something similar to example (methods should be as small as possible at this stage, so you could easily track, where program starts behave not in the way as you want):

And enjoy, as it should be a fun for you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!