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:
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!
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
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.
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.
Joined: Sep 14, 2012
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.
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.