File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Troublesome logic error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Troublesome logic error" Watch "Troublesome logic error" New topic
Author

Troublesome logic error

Eric Larsen
Ranch Hand

Joined: Mar 28, 2009
Posts: 35
The following app is designed to play music using javax.sound.midi. It has three menu items, new, save, and load. New and save work as expected, but load does not. The load menu item has an ActionListener, LoadMenuListener. LoadMenuListener's actionPerformed() method opens the JFileChooser and passes the selected file to loadFile(File file). In loadFile(File file), a BufferedReader is chained to a FileReader(file). Then, the BufferedReader reads the only line in the file. This line contains 0's and 1's separated by commas. String.split(",") is called on the line and the result is stored as an array of Strings. Then, the program steps through each element and does an if/else/if:

based on the results, it sets the element of checkBoxList in question (a JCheckBox) to either true or false.

That is what SHOULD happen. What REALLY happens however, is that it does not change the state of any JCheckBox in checkBoxList. It give's no run-time errors.

Thanks in advance for the help!

Here is the complete code:


Attached is a picture of the gui.


[Thumbnail for guiPic.JPG]

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You can't compare the contents of Strings using the "==" operator; this operator returns true if and only if two Strings represent the same physical object in memory, and a String read from a file will never be the same physical object as a literal embedded in a program (like your "0" and "1"). Instead, you want to compare the contents of the Strings; for this you must use the equals() method:



Beware of NullPointerExceptions -- you'll get one with this code if result[cellNum] is ever null.


[Jess in Action][AskingGoodQuestions]
Eric Larsen
Ranch Hand

Joined: Mar 28, 2009
Posts: 35
Thanks! I had a feeling it was something simple like that, but I had no idea where to look (as far as resources go) to find out what was wrong. The API only includes information specific to classes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Troublesome logic error