0

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

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by wildplace
0

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

0

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

0

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?

0

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.