String [] A = {"High","Medium","Low"};
String [] B = {"High","Medium","Low"};
String [] C = {"High","Medium","Low"};
String [] D = {"High","Medium","Low"};
String [] E = {"High","Medium","Low"};
String [] F = {"High","Medium","Low"};
JComboBox Ai = new JComboBox(A); JComboBox Bi = new JComboBox(B);
JComboBox Ci = new JComboBox(C); JComboBox Di = new JComboBox(C);
JComboBox Ei = new JComboBox(E); JComboBox Fi = new JComboBox(F);
....
//add the user choice in arrayList
ArrayList<String> a = new ArrayList<String>();
a.add((String) Ai.getSelectedItem());
a.add((String) Bi.getSelectedItem());
a.add((String) Ci.getSelectedItem());
a.add((String) Di.getSelectedItem());
a.add((String) Ei.getSelectedItem());
a.add((String) Fi.getSelectedItem());
EDITED:
Scenario:
There are 6 groups (Ai,Bi,Ci,Di,Ei,Fi) of choice. On each group, there are 3 sub choice (High(H),Medium(M),Low(L)).The user need to choose one on each of the 6 groups
The choice could be e.g. “HHHLLL” or “MMMLLM” or “HHLLMM” etc.
What is the best way to check and match the user choice without writing many else if ?
e.g.
if(Ai=="High" && Bi=="High" && Ci=="Low" && Di=="High" && Ei=="Low" && Fi=="Medium") {
System.out.println("Good Choice");
}
Thank you.
First off, you don’t need to give a new list of choices to each JComboBox.
(Variables in Java usually start with a lowercase letter, so I changed the variable names to lowercase.)
Next, you can put all six JComboBoxes in an array. You’ll see why this is useful in a moment.
Now, you can make your user choice string like this:
That for-each loop might not be familiar to you. It means “go through this code once for each something in array of somethings.”
If you use this code, you’ll end up with an ArrayList called userChoice that has something like [“H”, “H”, “M”, “M”, “L”, “L”].