Hi

I need some help.... Here is what Im trying to do....I havent worked with java in about 4 years.


I have created a JOptionpane showInputDialog with a list of options to choose from inside a method. What I want to do next is once one of those options is selected to call upon another method. Im not sure how to do that. I tried to create variables and assign them to the value of the object and then create an if statement to compair the two.

public static void selectTask()
   {
   //In initialization code:
    Object[] possibleValues = { "Print", "Borrow", "Return", "Quit" };
    Object selectedValue = JOptionPane.showInputDialog(null, "What Would You Like To Do?", "SELECT A TASK", JOptionPane.INFORMATION_MESSAGE, null,
    possibleValues, possibleValues[0]);}

I also tried to call upon the aray and that did not work either....

if 
    ((selectedValue != null) && (possibleValues =[1]))
   JOptionPane.showMessageDialog(null,"TEST TO SEE IF WORKS", "WELCOME",JOptionPane.PLAIN_MESSAGE);

Am I way off in what Im trying to do...Any help would be greatly appreciated....Or a suggestion on a simpler way to do this.

change (possibleValues =[1]) to (possibleValues[1].equals((String) selectedValue)) This, obviously, only checks the answer against the second element in the array, if you wish to check the value against all values in the array do as follows:

if (selectedValue != null) {
  for (int i = 0; i < possibleValues.length; i++) {
    if (possibleValues[1].equals((String) selectedValue)) {
      // do something
    }
  }
}

You may wish to read a few of the basic tutorilas again to brush up on your basic syntax (such as array indexing).

Thanks, It worked. Im still working on putting it in an array.... I will try latter tonite.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.