Hey, I'm currently writing a SUVAT calculator in Java, my program needs to know when the user enters a dash as an argument, my problem is essentially this code fragment:

if(args[x] != "-")
{
  valuesYouHave[x] = true;
  values[x] = Double.parseDouble(args[x]);
}

From my own testing I have deduced that the conditions on the if statement evaluate to true even if the argument is a dash, this obviously doesn't play nice with parseDouble and errors ensue. I can't see the problem with my if statement, but I haven't coded in a while over summer so I've inevitably missed something. Can anyone please help me?

Thanks,
Lolecule

Member Avatar

I'm not sure exactly why it wouldn't work, but couldn't you simply encase it in a try-catch?

Yes, I could encase it in a try-catch I guess, it's more just frustrating me that what should be a simple if statement isn't doing what I want it to :).

Member Avatar

Yeah, testing for equality has always been a bit of a problem. Is args a String array? If so, try .equals(), though I doubt it'd make a difference. Also, check to see if there's a regex expression for a dash?

Surprisingly .equals() worked! Thanks very much :).

wat u was doing is comparing args[x] and "-" refer to the same object, in this case args[x] always different from "-"
and .equal() is comparing if they are the same string