2 Replies Latest reply: Dec 15, 2007 5:44 AM by JoachimSauer RSS

    A problem with creating a switch case with a string

    807603
      I tried to create a switch to a string variable like so:

      switch (str) {
           case equals("yes"):
                     calc = true;
                               break;
           case equals("1"):
                calc = true;
                break;
           case "yeah":
                calc = true;
                break;
           case "no":
                calc = false;
                break;

           default:
           break;
                }
      System.out.print(calc);
      }


      and it wrote these error lines

      Exception in thread "main" java.lang.Error: Unresolved compilation problems:
           Cannot switch on a value of type String. Only int values or enum constants are permitted
           Cannot make a static reference to the non-static method equals(Object) from the type Object
           Cannot make a static reference to the non-static method equals(Object) from the type Object

           at Calculator.Main.main(Main.java:50)




      what to do? is there another way to create a switch case to a string?