• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Troublesome logic error

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
guiPic.JPG
[Thumbnail for guiPic.JPG]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic