• Post Reply Bookmark Topic Watch Topic
  • New Topic

HeadFirst Beatbox Sharpen your pencil  RSS feed

 
Glen Roberts
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've lurked around a long time and was trying to figure out something in the HeadFirst Java book and thought I'd ask for some input. If you're following along in the book Java HeadFirst page 464 wants you to use JFileChooser to save your beatbox "checkboxes". I'm just a little confused and was wondering if someone could explain/walk me through this.

My code is as follows


So I understand that there isn't anything being written. When I put the following code it won't allow me to write the boolean array. I feel like I should know what to do but drawing a blank. This isn't for an assignment just me learning so if you could walk me through it I'd greatly appreciate it. Just didn't want someone to post "use this code" and I still feel lost.





Thanks Again
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The file chooser doesn't save your file. What it does is allow you to find your file. Once the file is found you will have to write to it. You can read about file choosers in the Java® Tutorials. You appear to have a writing method in the second bit of code. Some style which needs improving however. You should change anything which looks like this:-to this:-What happens when you print a boolean[]? I know you can't simply writeWell, you can, but since arrays never override toString, you won't get a helpful output. You can try… and you can read about that method here. That will probably print
[true,false,false,true,...true] or similar.
In your line 10 you are using a “writer”. Is that a BufferedWriter? If so, remember that a BufferedWriter writes text. So if you get it to work your file will contain similar text to what I showed you earlier.

Alternative: Iterate the array and use the ?: operator to tell the writer what to write

You might do well to find out about Scanner and Formatter for reading and writing. You can use the %b and %n tags wtih Formatter for the output. You will have to work out what delimiter to use for a Scanner, but for the 256‑line version the default delimiter will probably do nicely. You can use the nextXXX methods of the Scanner to get different kinds of data from a text file.

If you have an array of check boxes, you don't need a boolean[]. You can simply iterate the box array and use its isSelected() method or similar to get the boolean, and you don't need the second array.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something went wrong with the first version of my post and a line about Scanner/Formatter went missing. I have reposted it, with the missing bit restored, and deleted the original post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!