• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if statement and combo box help

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic