Object serialization and try/catch

Posted 19 January 2013 - 12:52 AM

I am having some trouble understanding the order of execution in the try/catch statement. Staying close to the code in my textbook (for purposes of understanding their code), I have the following in my WriteData.java file:

When this executes, it wants to, for some reason, wait for my input and then prompt me for it! There has to be something wrong here! For some reason, the textbook put the prompt at the end of the while statement (and after all the try-catch statements), and I don't understand the flow of such statements. Also, in the ReadData.java file, I went closer to what they were trying to do. I don't feel comfortable with using the following infinite loop:

NO... if there is a failure you simply print it on the console and continue
the like if nothing happenned. No Exception throwed to inform the caller of this
method that the file couldn't be opened
Now how will the caller behave thinking that the file was opened is another story

In file processing, just doing a println() in a catch() clause is kind of not very useful

Re: Object serialization and try/catch

Posted 19 January 2013 - 01:04 PM

Sorry...I mixed up the names. It is now fixed both on here and on my computer. I think this answers most of my questions. pbl, good point: I should get right on that. Also, I have no idea why a renowned textbook is using an infinite while-loop, but I now know a way to avoid using one. This is a good thing because my game needs this file to analyze the player's performance in the beta-test. Also, my unanswered question is a rather trivial one because the game will be doing the input, not the user.

Re: Object serialization and try/catch

Posted 20 January 2013 - 12:17 AM

Sorry...had to think it over...I typed it before fully considering your answer, g00se. (I should have just let it sit.) Anyways, considering that answer, I still plan on making the modifications to the game such that it will capture the results that Joe User ends up with at the end of each game. Avoiding the while-loop, I see I have three options (which will have the same effect):
1.) save the number of times the player has played the game to the first object written in the .ser file and increment this number every time a game has been completed by setting the write "pointer" to that object
2.) same as number 1 except bring in EVERY LAST INSTANCE INTO AN ARRAY, modify the first one, and write the whole array to the file (assuming the user plays n times, that will result in a complexity of O(Math.pow(n,2)))
3.) write two .ser files; the first one having just this number of times played and the second one having the rest of the data. Implementations for this idea would be to either have two different objects or just overload the constructor so that this number can be part of a constructor.

The third object seems the most seductive one as it sounds easier than the other two. I am wondering if it will be the BEST option. What do you all think?

Re: Object serialization and try/catch

Posted 29 January 2013 - 09:26 PM

That error is solved, I think. Now, I am having trouble the game being played multiple times. It works when I simulate just one time the game is run, but any more than that, and it throws the following type of error:

After some searching, http://stackoverflow...id-type-code-ac, I found some explanation, but what gets me is the fact that I feel FORCED to code the way that is triggering that error. The data that is displaying isn't all the way correct, either. Does anyone on here know another way to write and fetch data written to files when the user decides to end the program? This is what I ended up doing: