Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with EnglishCalculator class  RSS feed

 
El Bel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written the following to take a string and perform calculations from the SimpleCalculator class, however it only returns -1. Any suggestions why much appreciated. Thanks!


 
Henry Wong
author
Sheriff
Posts: 23283
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
El Bel wrote:I've written the following to take a string and perform calculations from the SimpleCalculator class, however it only returns -1.


Isn't "4 minus 5" equal to -1? ...


Having said that though, your issue is likely related to your string comparison. I would recommend that you use the equals() method, instead of the "==" comparison operator.

Henry
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Strings in a switch statement. That might be a better way to implement the arithmetic.
Another way is to use an enumerated type. There is an example in the Java® Language Specification (=JLS) which does arithmetic. That is one of the parts of the JLS most people can actually understand You can get a constant out of the enumerated type with its valueOf method.

And welcome to the Ranch
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what happens when you use equality operator == OR equals() method to compare two Strings.
Output:
strOne == strTwo: false
strOne.equals(strTwo): true
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!