![]() |
Java: cannot figure why code is not logically correct
whenever I enter y or Y and call the method it should print out OK, but it doesn't it skips all the else if's and executes the last else statement "terminating" any suggestions why? i included the import java.lang.String .....thanks
public void checkVariable() { if(checkString == "y" || checkString == "Y") { System.out.println("OK"); } else if(checkString == "yes" || checkString == "Yes") { System.out.println("OK"); } else if(checkString == "ok" || checkString == "OK") { System.out.println("OK"); } else if(checkString == "sure" || checkString == "Sure") { System.out.println("OK"); } else if(checkString == "why not?" || checkString == "Why not?") { System.out.println("OK"); } else if(checkString == "n" || checkString == "N" || checkString == "no" || checkString == "No") { System.out.println("OK"); } else { System.out.println("Terminating"); } } |
show the input part of the code...that might be the error. Are you inputing a string or a character? you also might look at a method called toUpperCase or something like that, it converts all characters to uppercase so there will be only 1 check without all the or's ( || ) statments...
Jason |
i found out what was wrong
i was using strings.....i used the checkString.equals("y") and now it works! thanks
|
Yup. Just for general knowledge.
This condition: Code:
(someString == "a string")shmoove |
| All times are GMT +1. The time now is 03:52 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.