Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes if statement and combo box help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if statement and combo box help" Watch "if statement and combo box help" New topic
Author

if statement and combo box help

Candy Bortniker
Ranch Hand

Joined: Mar 17, 2003
Posts: 123
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");
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
 
wood burning stoves
 
subject: if statement and combo box help