Wednesday, January 20, 2010

How to use switch/cases in java to accept and pass strings?

Switch case in any OOP language can accept only integers and characters.How to use switch/cases in java to accept and pass strings?
The first answer to your question is correct. java will NOT accept String in a switch. don't forget that char is single quote.





char condition = 'b';


decideMe( condition );





public decideMe( char condition) {


switch( condition ) {


case 'a' : System.out.println(';Houston, we have an A';);


break;


default : System.out.println(';Houston, we have a problem';);


}


}





And, to convert back to char from a String takes a charArray();





java is picky on the Types of vars (which is good);How to use switch/cases in java to accept and pass strings?
switch(STRING VARIABLE)


{


case 'VALUE1':COMMAND1;break;


case 'VALUE2':COMMAND2;break;


case 'VALUE3':COMMAND3;break;


default: COMMAND4;//it is executed if non of above 3 cases do not meet your condition.


//STRING VARIABLE should be a char.


}
gwalker30...Read it and learn. Here is your link





http://www.google.co.uk/search?hl=en%26amp;q=u鈥?/a>

No comments:

Post a Comment