Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with syntax-If Statements

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Just transferring over form other programming languages...and just can't find out the syntax to this:

I want to compare a string argument in Upper/Lower case, but my code is off:

if ((args[3] == "P") || (args[1] == "p")) {
weight=weight/2.2;
}
if ((args[1] == "I") || (args[3] == "i")) {
height=height/39.36;
}
what needs to change?

BMS, frustratingly lost/...
 
In future posts you need to state what problem you are having ie. Compile-time error, Runtime Exception, or Logical Error.

My main suggestion, since I don't know the intimate details of your program, is that you need to be using equals() instead of ==. In Java == compares the memory addresses of two object (ie are they the same object). The method equals() compares the contents. I am assuming you want to compare content. So you code would be:
Code:
if ((args[3].equals("P")) || (args[1].equals("p")))  {
        weight=weight/2.2;
    }
if ((args[1].equals("I")) || (args[3].equals("i"))) {
        height=height/39.36;
    }
Futhermore you could do a bit better and ensure a NullPointerException not thrown if one of the agruments is null with the following:
Code:
if (("P".equals(args[3])) || ("p".equals(args[1])))  {
        weight=weight/2.2;
    }
if (("I".equals(args[1])) || ("i".equals(args[3]))) {
        height=height/39.36;
    }
Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top