jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if statement and combo box help" Watch "if statement and combo box help" New topic

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"))
else if (StockName.getSelectedItem().equals("Archer Daniels Midland Company"))
else if (StockName.getSelectedItem().equals("AT&T"))
else if (StockName.getSelectedItem().equals("ChevronTexaco Corporation"))
else if (StockName.getSelectedItem().equals("Cisco Systems Inc"))
else if (StockName.getSelectedItem().equals("Intel Corporation"))
else if (StockName.getSelectedItem().equals("General Electric Company"))
else if (StockName.getSelectedItem().equals("Global Crossing Ltd"))
else if (StockName.getSelectedItem().equals("K Mart Corporation"))
else if (StockName.getSelectedItem().equals("Microsoft Corporation"))
else if (StockName.getSelectedItem().equals("Palm Inc"))
else if (StockName.getSelectedItem().equals("Providian Financial Corporation"))
else if (StockName.getSelectedItem().equals("Qwest Communications International Inc"))
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
I agree. Here's the link: http://aspose.com/file-tools
subject: if statement and combo box help
It's not a secret anymore!