Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem matching command line to strings  RSS feed

 
Derek Roberts
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,


I'm really stuck on what I hope is a small problem. I've written a command line calculator for school that works just fine for values I put into the main method, but is not matching my operators when entered from the command line. In the example here it's expecting that the 3rd (position 2) argument received is a plus sign. Could somebody please help me to get a match?

Command line => java Calc 4 5 +



I've also tried putting (String) in front of args[2]. If somebody could help it would be greatly appreciated. Thanks!


Best regards,

Derek
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try using the equals() methods to compare strings.

Henry
 
Derek Roberts
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Henry,


Wow, you answered my question five minutes after it was posted. Trivial or not (okay, I admit it's trivial...) that's fantastic. Thank you very much!

Code corrected to:

Best regards,

Derek
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Roberts wrote: . . . Wow, you answered my question five minutes after it was posted. Trivial or not (okay, I admit it's trivial...) that's fantastic. Thank you very much! . . .
We try our hardest . . .

It's not a trivial question, but a useful question. Lots of people make that mistake. I have seen experienced people do the same; I came across one person who had lots of C# experience, where == is overloaded for Strings. Using == on strings worked at first, because all the Strings were instantiated from String literals, but after serialization and deserialization, it no longer worked.

The link about "String literals" is probably helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!