RE: expression ALWAYS evaluates to if... NEVER to else
You'll want to do if( !(paramPassword.equalsIgnoreCase(secretCode))) { } else { } You cant use logical operators on Strings for what you're trying to do! Look up the java.lang.String javadocs for more information on the equals and equalsIgnoreCase methods. Steve -Original Message- From: Z.BEAT [mailto:zackbeatty;yahoo.com] Sent: Thursday, October 17, 2002 1:58 PM To: Tomcat Users List Subject: expression ALWAYS evaluates to if... NEVER to else In the following code snippet, the expression ALWAYS evaluates to the if statement block: String paramPassword = request.getParameter(paramPassword); String secretCode = secret; if(paramPassword != secretCode) { } else { } However, my debugging flags that I send in an HTML comment CLEARLY show that the two variables have the same value: !-- DEBUG FLAGS paramPassword: secret secretCode: secret -- What is going on? Am I missing something obvious? Thanks! __ Do you Yahoo!? Faith Hill - Exclusive Performances, Videos More http://faith.yahoo.com -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org
RE: expression ALWAYS evaluates to if... NEVER to else
You can't use != or == to compare Strings. http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html You could use something like: if (paramPassword.compareTo(secretCode) == 0) { // they match } else { // they don't } Or flip your if around and use .equals(): if (paramPassword.equals(secretCode)) { // they match } else { // they don't } So, in your current if statement, the condition is always true, as a FALSE (that they match) condition can never happen. John -Original Message- From: Z.BEAT [mailto:zackbeatty;yahoo.com] Sent: Thursday, October 17, 2002 1:58 PM To: Tomcat Users List Subject: expression ALWAYS evaluates to if... NEVER to else In the following code snippet, the expression ALWAYS evaluates to the if statement block: String paramPassword = request.getParameter(paramPassword); String secretCode = secret; if(paramPassword != secretCode) { } else { } However, my debugging flags that I send in an HTML comment CLEARLY show that the two variables have the same value: !-- DEBUG FLAGS paramPassword: secret secretCode: secret -- What is going on? Am I missing something obvious? Thanks! __ Do you Yahoo!? Faith Hill - Exclusive Performances, Videos More http://faith.yahoo.com -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org
Re: expression ALWAYS evaluates to if... NEVER to else
Hello Z.BEAT, If these are both java String objects then all you are comparing is if the memory location of string object #1 is the same as that of String object #2. This is probably not true. You need to compare like this. if (paramPassword.equals(secretCode)) { } else { } You can only count on the == operator to compare an object to null or to compare simple data types such as int, boolean, long, etc Jake Thursday, October 17, 2002, 12:57:35 PM, you wrote: ZB In the following code snippet, the expression ALWAYS ZB evaluates to the if statement block: ZB String paramPassword = ZB request.getParameter(paramPassword); ZB String secretCode = secret; ZB if(paramPassword != secretCode) ZB { ZB } ZB else ZB { ZB } ZB However, my debugging flags that I send in an HTML ZB comment CLEARLY show that the two variables have the ZB same value: ZB !-- DEBUG FLAGS ZB paramPassword: secret ZBsecretCode: secret -- ZB What is going on? Am I missing something obvious? ZB Thanks! ZB __ ZB Do you Yahoo!? ZB Faith Hill - Exclusive Performances, Videos More ZB http://faith.yahoo.com ZB -- ZB To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org ZB For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org -- Best regards, Jacobmailto:hoju;visi.com -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org
RE: expression ALWAYS evaluates to if... NEVER to else
You need to learn how java compares objects and strings. The short story is: if(!paramPassword.equals(secretCode)) { } else { } -Original Message- From: Z.BEAT [mailto:zackbeatty;yahoo.com] Sent: 17 October, 2002 11:58 AM To: Tomcat Users List Subject: expression ALWAYS evaluates to if... NEVER to else In the following code snippet, the expression ALWAYS evaluates to the if statement block: String paramPassword = request.getParameter(paramPassword); String secretCode = secret; if(paramPassword != secretCode) { } else { } However, my debugging flags that I send in an HTML comment CLEARLY show that the two variables have the same value: !-- DEBUG FLAGS paramPassword: secret secretCode: secret -- What is going on? Am I missing something obvious? Thanks! __ Do you Yahoo!? Faith Hill - Exclusive Performances, Videos More http://faith.yahoo.com -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org
RE: expression ALWAYS evaluates to if... NEVER to else
Actually, you can use == to compare strings but only if the strings are created by; String x = ; and not by; String x = new String(). It has to do with the where java stores the string object (literal pool vs programmers space). But either way, you should always use the equals method to compare two object as long as the overriding equals method your using compares what you want it to compare. Alan Sparago [EMAIL PROTECTED] 602 494 9596 -Original Message- From: Jacob Kjome [mailto:hoju;visi.com] Sent: Thursday, October 17, 2002 11:06 AM To: Tomcat Users List Subject: Re: expression ALWAYS evaluates to if... NEVER to else Hello Z.BEAT, If these are both java String objects then all you are comparing is if the memory location of string object #1 is the same as that of String object #2. This is probably not true. You need to compare like this. if (paramPassword.equals(secretCode)) { } else { } You can only count on the == operator to compare an object to null or to compare simple data types such as int, boolean, long, etc Jake Thursday, October 17, 2002, 12:57:35 PM, you wrote: ZB In the following code snippet, the expression ALWAYS ZB evaluates to the if statement block: ZB String paramPassword = ZB request.getParameter(paramPassword); ZB String secretCode = secret; ZB if(paramPassword != secretCode) ZB { ZB } ZB else ZB { ZB } ZB However, my debugging flags that I send in an HTML ZB comment CLEARLY show that the two variables have the ZB same value: ZB !-- DEBUG FLAGS ZB paramPassword: secret ZBsecretCode: secret -- ZB What is going on? Am I missing something obvious? ZB Thanks! ZB __ ZB Do you Yahoo!? ZB Faith Hill - Exclusive Performances, Videos More ZB http://faith.yahoo.com ZB -- ZB To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org ZB For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org -- Best regards, Jacobmailto:hoju;visi.com -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org
RE: expression ALWAYS evaluates to if... NEVER to else
My apologies for the easy question. I should have known. --- [EMAIL PROTECTED] wrote: You'll want to do if( !(paramPassword.equalsIgnoreCase(secretCode))) { } else { } You cant use logical operators on Strings for what you're trying to do! Look up the java.lang.String javadocs for more information on the equals and equalsIgnoreCase methods. Steve -Original Message- From: Z.BEAT [mailto:zackbeatty;yahoo.com] Sent: Thursday, October 17, 2002 1:58 PM To: Tomcat Users List Subject: expression ALWAYS evaluates to if... NEVER to else In the following code snippet, the expression ALWAYS evaluates to the if statement block: String paramPassword = request.getParameter(paramPassword); String secretCode = secret; if(paramPassword != secretCode) { } else { } However, my debugging flags that I send in an HTML comment CLEARLY show that the two variables have the same value: !-- DEBUG FLAGS paramPassword: secret secretCode: secret -- What is going on? Am I missing something obvious? Thanks! __ Do you Yahoo!? Faith Hill - Exclusive Performances, Videos More http://faith.yahoo.com -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org __ Do you Yahoo!? Faith Hill - Exclusive Performances, Videos More http://faith.yahoo.com -- To unsubscribe, e-mail: mailto:tomcat-user-unsubscribe;jakarta.apache.org For additional commands, e-mail: mailto:tomcat-user-help;jakarta.apache.org