0

on my personal quest to learn some Java I have come across another hurdle.

So far I have 2 arrays, the first one is a 1D array with a list of photographic film manufacturers, the second is a 2d array with the following appearance:

Film               Developer    Dilusion   35mm    120    Sheet    Temperature
Kodak T-Max 100,   Ilfosol S,   1+9,       10,     10,    ,        20C
Kodak T-Max 100,   Rodinal RO9, 1+25,      9,      9,     ,        20C
Kodak T-Max 100,   Rodinal RO9, 1+50,      13,     13,    ,        20C

(nb, don't use the above times for developing those films, I've just input any data for this example)

On my JFrame I have a list of JComboBoxes (film, developer, dilution, film format). The film box is populated by the 1D array and I have the following code in an attempt to list the possible developers when a film is chosen. At present this code causes an endless loop requiring a hard reset of my laptop, so something definitely isn't right.

private void filmComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
    developerComboBox.removeAllItems();

    int count = 0;
    while (count <= rows) {
        if (myArray[0][count] == filmComboBox.getSelectedItem()) {
            developerComboBox.addItem(myArray[1][count]);
            count++;
        }
    }
}          

Any advice on where I am going wrong with this code is much appreciated, once this part is done I will be repeating the code with different string names etc... to populate the dilution field when a developer is selected, then the film format field

Cheers Jim

TheVDM
  • 29
  • 6

2 Answers2

1
while (count <= rows) {//Loop until count less than or equal to rows
        if (myArray[0][count] == filmComboBox.getSelectedItem()) {
         //If it Never Satisfied Count won't be incremented
            developerComboBox.addItem(myArray[1][count]);
            count++;//Never Incremented
        }
    }

You count variable is not incrementing and your loop becomes infine put count++ outside your if block and use .equals method to compare Strings in Java.

Now you may ask Why do I compare Strings with equals in java?

Community
  • 1
  • 1
akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    Thank you for your help, that makes perfect sense to me now. I think I will blame the heat for that one. Also thank you for posting the link containing the details of == and .equals(), that will come in very handy. Jim – TheVDM Jul 26 '14 at 13:46
0

Put the count++ outside of the if block! The count variable will not increment if the if block is not true, and you'll re-test the same false boolean statement over and over again. Also don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

do,

if ("bar".equals(fu)) {
  // do something
}

or,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373