aspose file tools*
The moose likes Beginning Java and the fly likes Putting array info into a .txt file: a bunch of zeros and Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Putting array info into a .txt file: a bunch of zeros and "null" instead of integers & Strings" Watch "Putting array info into a .txt file: a bunch of zeros and "null" instead of integers & Strings" New topic
Author

Putting array info into a .txt file: a bunch of zeros and "null" instead of integers & Strings

Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
In this program, the user is required to input employee names (Strings) and payment info(integers), and it's all put into a 2-dimensional array as a table. I'm trying to get the program to write the array info to a .txt file. However, when I open the .txt file, everything is all messed up, with a bunch of integers replaced with "0" and Strings replaced with "null". For example:

dan,
23,5,1,2,3,4,5null,
0,0,0,0,0,0,0null,
0,0,0,0,0,0,0

the info should actually be like this:

dan,23,5,1,2,3,4,5
terry,34,7,2,5,3,5,8
joe,45,6,4,2,9,3,6

The program's full code is below. Any >< is supposed to be < (I don't know why it does that when I put the code into the forums). The part that's supposed to write the info to a .txt file is lines 130-157. Help would be appreciated, thanks!


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

You might want to consider trimming that down. I doubt many people are going to try and dig through over 400 lines of code to solve your problem. If you could strip out any irrelevant parts, but leave a working program that we can compile and run, you are MUCH more likely to get the assistance you are requesting.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're trying to deal with too much at once.

Start small. Create a program that just hardcoded entries into a single, 1D array, and then writes that array out. Print out the value of the array to the console so you can confirm that the array's contents are what you expect.

Once that's working, move up to a simple hardcoded 2D array, but keep it small, say 2 rows by 3 columns. Again, print out all the contents to the terminal, and print out any other significant variables, code branches taken, etc., so you can see what your code is actually doing.

Once that works, add the piece that takes user input. And before you do that, you'll be better off if you develop that piece all by itself, as a separate program that takes user input, populates an arary, and prints it out to the screen, without any regard to saving it to files. Once you get that piece working by itself, it will be easier to combine it with the other working piece.

And if you get stuck, create a program that shows ONLY your problem and has no unrelated cruft--an SSCCE(←click) and post it here.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Well, little off topic, but I must notice - this is one long, long main method...
By the way, I think I've seen this code here and I also think we suggested to OP to organize it a little bit better.


The quieter you are, the more you are able to hear.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39810
    
  28
You have five lots of almost identical code, from line 379. That looks like something which ought to have been a loop calling another method 5×. I haven’t even looked at the rest of it, but I suspect there are similar repetitions throughout.
Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
fred rosenberger wrote:You might want to consider trimming that down. I doubt many people are going to try and dig through over 400 lines of code to solve your problem. If you could strip out any irrelevant parts, but leave a working program that we can compile and run, you are MUCH more likely to get the assistance you are requesting.
Just trimmed it down. It's half as long now.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Ok, now try to initialize your PrintWriter with FileWriter, as:


Why are you outputting everything three times in that for loop (130-155)? And why did you hard code those indexes? What if you decide to have 4 of them later, you're just gonna repeat the same code again? You can as well put that code outside that for loop and output the result just once. There are lots of ways to avoid that code repetition you have in what you've posted. E.g. why don't you toString() method in your class and just invoke it each time you want to output it to the console, file or whatever? In combination with for loop and separate method the code would look much better.

Again, you should really consider redisigning your class(es) (some of the tips are given in your previous topic). You seem to be handling this code for some time now, and all you try to do is put almost everything in that main method and just make it work, no matter how.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

//tried to loop, didn't work

you surely don't expect us to be able to help you without seeing what you did, do you? You should probably read our FAQ on HowToAskQuestionsOnJavaRanch, and specifically ItDoesntWorkIsUseless.

I will GUESS that you used a single for loop. If you have and array of arrays, you would need two loops.
 
Consider Paul's rocket mass heater.
 
subject: Putting array info into a .txt file: a bunch of zeros and "null" instead of integers & Strings