Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java QuizCardBuilder not saving files correctly

 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm learning java from the Head First book.

I've been trying to make their QuizCardBuilder and QuizCardPlayer programs, but the QuizCardPlayer is throwing an exception when I try to load a card set:

c:\java>java QuizCardPlayer
make a card
couldn't read the card file


I think it has to do with how the other program, the QuizCardBuilder is saving files.

Here's the code for the saveFile method:

If I open up the saved file and just look at it in notepad, the new line doesn't appear to get added after the answer. Instead, I just see another /, similar to what is added after the Question. In fact, if I chance the /n to something else, it still adds a /, so I'm not sure what's going on with that. Does anyone have any idea what's going on here?

Thanks
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly...Welcome to Java Ranch!

Now that is out of the way, if you could please post at the very least the method which is creating the error itself, looks like it is the makeCard method. It has been awhile since I did the Head First Java examples.

Also, please use the code boxes built into the forums when placing code in your posts, it makes it easier to read and go through which in turn makes your posts easier to reply to.
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my lack of etiquette!

I think there are 2 methods related to loading the cards, I'll also include the code for the showNextCard:


Thanks a lot for your help, I really appreciate it!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags to your first post; you can see how much better it looks. Which is line 102?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I have added code tags to your first post; you can see how much better it looks. Which is line 102?


I'd bet it was line 31 in his second post and that lineToParse.split("/") is returning an empty array. i think that is only possible if lineToParse is an empty String. Does your input file contain any blank lines ?

Edit: Just tried it and split on an empty String returns an array of length 1. I can't see anything else in the makeCard method that would throw an ArrayIndexOutOfBoundsException exception so there must be something that causes split to return an empty array. Can you put a System.out.println() call before line 31 that shows the value of lineToParse.

Edit 2: split will return an empty array if lineToParse contains just / characters. Do you have such a line in your input file ?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you have a 1-element array, and try to get [1] on it . . . (Also line 31)
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the text from the saved file:

what is best?/blah blah blah
/
what is super?/yada yada yada
/
/


Hmm, in notepad, it doesn't appear to have a new line, but when I copy it into here, it looks like the new line is there.

In the example in the book, it's not supposed to add the / after every line.

Ok, here's all the code for QuizCardBuilder:



I added the println to print out the string array result. When I try to load a card, this is what I get:



It looks like it's loading the first card correctly, but then not the second one, right?

Thanks again for your help!
 
Sudipta Laha
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Give a SOP in line 88 before calling the method. You will get to know whats coming in the line and track the issue.
If / is the issue . you can modify your file or put some validation in your method
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

When I add an SOP before line 88, this is what I get:



It's reading the first line ok, but when it tries to read the second line, it gets to the / and that messes up the split line. So I still don't understand why the QuizCardBuilder program is adding / after every line when it creates the saved file.

Here's the code for that method again:



After it writes the text from getAnswer, it should just add an new line, not a /, right?

Many many thanks for all your help.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean you are writing the results into a file before you read them? Joanne and I have (I think) already given the explanation for the out of bounds Exception. But you need to work out why you are getting such a short line.

Which page is this exercise on?
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The QuizCardBuilder exercise is on p.450, and the QuizCardPlayer exercise is on p.456.

The goal of the exercises is to learn how to write to text files and read from text files.
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'm getting a short line because the QuizCardBuilder is writing to the file incorrectly, adding a / after the \n. Does the write method from the JAVA API automatically add a / when it writes a line or something like that?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to check the API for the write method, but as far as I remember, no it doesn't add a /. You would appear to be getting one more / than you need.
Put this block immediately after your last "write" instruction. Beware: I have found some spelling errors in it; there might be some I haven't noticed.That will give you a 30-second delay, during which you can try and open the destination file with a text editor and check the last line for whether it is question/answer or only /.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But if you have a 1-element array, and try to get [1] on it . . . (Also line 31)


True, but the exception tells you which element is out of bounds.

Matthew Busse wrote:
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I missed that bit. Sorry.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dugie notvalid,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic