0

I want run different classes based on a variable value .. but when I chose the first value it run all classes .. and when I chose second value it runs two last classes and when I chose thirth value it run the last class ..where is the problem in my code ..thanks

String[] args = null;
        String y =  ps.getSelectedtype() ;
        String[] keys = {"Type1", "Type2", "Type3"};
        String[] values = {"conditionA", "conditionB", "conditionC"};
        String match = "default";

                for (int i = 0; i < keys.length; i++) {
                    if (y.contains(keys[i])) {
                        match = values[i];
                        break;
                    }
                }
                switch (match) {
                case "conditionA":
                    FirstClass.main(args);
                case "conditionB":
                     SecondClass.main(args);
                case "conditionC":
                     ThirthClass.main(args);
             }
Khaaled
  • 1
  • 1
  • 5

1 Answers1

0

You are missing a break after the method execution.

switch (match) {
    case "conditionA":
        FirstClass.main(args);
        break;
    case "conditionB":
        SecondClass.main(args);
        break;
     case "conditionC":
        ThirthClass.main(args);
        break;
}

Take a look here to understand how the switch statement works: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

pedrohreis
  • 1,030
  • 2
  • 14
  • 33