• Post Reply Bookmark Topic Watch Topic
  • New Topic

if and logic  RSS feed

 
Roger Wells
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble with this if statement. What I want to happen is if testField  ==  "<t" and t3 |= a 1 then do something

testField is a string
t3 is a int

if (testField = "<t" && t3 |=1) {
               // do something
           }

Do both testField and t3 have to be both strings or both int? Also I am not sure about the &&

Roger
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to do something in the block of If statement If
testField is equals to "<t" where "<t" is a String literal and t3 is not equals to 1 where 1 is int value, am I correct, Is this what you want? If not then please describe in textual way what precisely do you want to do?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several things are wrong with the if statement:
  • You probably mean "==" and "!=".  These are logical equals and not equals.
  • Never write strVar == anything, write strVar.equals(anything)
  •  
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i'm not sure I understand what your second condition is supposed to be.

    regardless...

    the "thing" inside the the parentesis of an if-statement must eventually evaluate to a boolean - true or false. How you get there is irrelevant. so no, testfield and t3 do not both have to be both Strings or ints.

    So let's back up...

    You start with this:



    a single equal sign is an ASSIGNMENT, not a compare.  You are changing the value of testField to contain the literal "><t".  So that's your first issue. When comparing Strings, you want to use the .equals method:



    so make that change. I'd take out the rest for now, and get that to work.

    Next, you state you want a condition of "t3 |= a 1"  that doesn't make sense to me.  what is |=?  what is 'a'?  what is '1'? Can you just write out in English what you want here?

    Don't worry about getting both conditions to work together until you figure out how to get them to work separately.

    >
     
    Julian West
    Ranch Hand
    Posts: 91
    3
    Chrome Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As was said "==", not "=" and "!=" (exclamation point) for "not equals" for primitives.  For strings, "==" won't work except it narrow circumstances; use .equals().  Try changing the code below to "==" and see what happens.

     
    Roger Wells
    Ranch Hand
    Posts: 51
    1
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for all of the help

    I finally got it to work. One of the things I really missed was the .equal vs == also some of my posting had horrid typing mistakes.

    The suggestion that I get each if to work by it's self really helped in finding out how it really worked.

    This how I finally ended up with only using the last one printing out working 3 being used.

     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done Roger Worth reading click here  ---> Strings, Literally by Corey McGlone you can thank me later  
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roger Wells wrote:Thanks for all of the help

    I finally got it to work...This how I finally ended up with...

    Thanks for coming back and telling us what you did and what worked. That deserves a cow!!!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!