Forums Register Login

if statement and combo box help

+Pie Number of slices to send: Send
I have a GUI that has a combo box where you make a selection, when the use selects something in the combo box, another text box is filled with a code. This all works well except for the first selection in the if statement. What is the reason for it not to work and what can I do to fix it?
if (StockName.getSelectedItem().equals("American Electric Power Company"))
Symbol.setText("AEP");
else if (StockName.getSelectedItem().equals("Archer Daniels Midland Company"))
Symbol.setText("ADM");
else if (StockName.getSelectedItem().equals("AT&T"))
Symbol.setText("T");
else if (StockName.getSelectedItem().equals("ChevronTexaco Corporation"))
Symbol.setText("CVX");
else if (StockName.getSelectedItem().equals("Cisco Systems Inc"))
Symbol.setText("CSCO");
else if (StockName.getSelectedItem().equals("Intel Corporation"))
Symbol.setText("INTC");
else if (StockName.getSelectedItem().equals("General Electric Company"))
Symbol.setText("GE");
else if (StockName.getSelectedItem().equals("Global Crossing Ltd"))
Symbol.setText("GX");
else if (StockName.getSelectedItem().equals("K Mart Corporation"))
Symbol.setText("KM");
else if (StockName.getSelectedItem().equals("Microsoft Corporation"))
Symbol.setText("MSFT");
else if (StockName.getSelectedItem().equals("Palm Inc"))
Symbol.setText("PALM");
else if (StockName.getSelectedItem().equals("Providian Financial Corporation"))
Symbol.setText("PVN");
else if (StockName.getSelectedItem().equals("Qwest Communications International Inc"))
Symbol.setText("Q");
+Pie Number of slices to send: Send
Hello again Candy,
Is the problem that the line Symbol.setText("AEP") never gets set, even when you feel that StockName.getSelectedItem() should return "American Electric Power Company"? If that's the case then probably the value returned by the method is not what you think it is for some reason. It could be that there is some white space at either the beginning or end of the string. If so, you can change the statement to if (StockName.getSelectedItem().trim().equals.("American Electric Power Company")) and that will fix the problem. You can also put a System.out.println(StockName.getSelectedItem()) right before the if statement and see what pops up on the console. Also, I would strongly advise you to enclose all if blocks with curly braces {} even when there is only one statement to execute. For one thing, it makes the code more readable and if you later need to add a line to that if block you won't get a syntax error or worse a statement that executes whether the if evaluates true or false.
Michael Morris
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6118 times.
Similar Threads
help!! on dynamic select box - 2
Processing with two JComboBox's in one JPanel/JFrame
Swings
IllegalArgumentException
How to load data in a combo based on user selection on another combo
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:07:27.