1

making a website in JSP on eclipse:

select[i] is acquired from the previous webpage correctly as a string from 1 to 5

each number represents a subject ie: if select[i]==1 so sub=Maths

I can not switch case on a string so I tried if else .. but sub is always equal to null (the declaration) ?? how can i make sub take the values in the if condition??

  for (int i = 0; i < select.length; i++) 
        {   

            ////
            String sub=null;
            if(select[i]=="1") {sub="Maths";}
            else if (select[i]=="2") {sub="English";}
            else if (select[i]=="3") {sub="Physics";}
            else if (select[i]=="4") {sub="MI";}
            else if (select[i]=="5") {sub="Software";}
            ////

            rs=stmt.executeQuery("SELECT * FROM attends where userid= '"+user_id+"' and cid= '"+select[i]+"'  ");



            if(rs.next())//can not take it
                {
                out.println("You can not enroll in '"+sub+"' ");
                }
            else//can take it
                {
                int countUpdated =stmt.executeUpdate("INSERT INTO enroll (userid, cid) values ('"+user_id+"', '"+select[i]+"')");
                out.println("Successfully enrolled in '"+sub+"' ");
                }



        }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
CodeX
  • 135
  • 2
  • 13

2 Answers2

2

This is one of the first problems I ever ran into while learning Java: the quandary of == vs equals. Fortunately, once you understand why they're different, it's easy to use them properly.

Whenever you're dealing with objects (as you are in this case), the == operator is used to determine whether two variables actually point to the same object. Objects are dealt with by reference in Java, so if object1 == object2, then the variable object1 actually refers to the same object that the variable object2 refers to.

This is not what you want here. You're trying to determine not whether two String variables point to the same object, but rather whether their contents are the same. For that, you should use the equals method, like so:

String sub=null;
if(select[i].equals("1")) {sub="Maths";}
else if (select[i].equals("2")) {sub="English";}
else if (select[i].equals("3")) {sub="Physics";}
else if (select[i].equals("4")) {sub="MI";}
else if (select[i].equals("5")) {sub="Software";}

This allows you to test whether the contents of sub are the same as the string "1", "2", etc.

And I believe your assumption about switch statements is incorrect: you can switch on a String in Java, and the equals method is used under the hood. So something like this:

String sub;
switch (select[i]) {
  case "1":
    sub = "Maths";
    break;
  case "2":
    sub = "English";
    break;
  case "3":
    sub = "Physics";
    break;
  case "4":
    sub = "MI";
    break;
  case "5":
    sub = "Software";
    break;
  default:
    sub = null;
    break;
}

might be preferable, because that's what switch statements are designed for.

Sam Estep
  • 12,974
  • 2
  • 37
  • 75
0

Try this like

switch(select[i]) {
            case "1":
              sub="Maths";
              break;
           case "2":
              sub="English";
              break;
 case "3":
              sub="Physics";
              break;
 case "4":
              sub="MI";
              break;
 case "5":
              sub="Software";
              break;
default:
            sub="";
             break
}
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
  • gives the error HTTP Status 500 - Unable to compile class for JSP .. can not wsitch case on a string only on enum and int variables – CodeX Jun 27 '15 at 13:44
  • @CodeX As of Java 7, it is possible to switch on Strings. Refer to this [Oracle documentation](https://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.html) on the subject. Which JDK are you using on your server? – Parker Jun 27 '15 at 15:20