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?


8 Years
Discussion Span
Last Post by wildplace

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 :).


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?


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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.