Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2004, 10:25 PM   PM User | #1
bennycs
New Coder

 
Join Date: Jun 2003
Location: USA
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
bennycs is an unknown quantity at this point
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");
}
}
bennycs is offline   Reply With Quote
Old 02-05-2004, 11:00 PM   PM User | #2
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Jason is offline   Reply With Quote
Old 02-05-2004, 11:02 PM   PM User | #3
bennycs
New Coder

 
Join Date: Jun 2003
Location: USA
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
bennycs is an unknown quantity at this point
i found out what was wrong

i was using strings.....i used the checkString.equals("y") and now it works! thanks
bennycs is offline   Reply With Quote
Old 02-06-2004, 05:39 PM   PM User | #4
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
Yup. Just for general knowledge.
This condition:
Code:
(someString == "a string")
will always return false in Java, because a string is a final object in java, so every time you assign a string you are actually making a copy of it in a different object, and the condition is checking if someString is a reference to the same object as the constant string, which can't be true.

shmoove
shmoove is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:06 AM.


Advertisement
Log in to turn off these ads.