• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why the String value is not read?  RSS feed

 
Alex Manfredi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

This code is part of the assgnment I am working on. It changes the comission of a sales assistant according to the code of the product.

I've done one that reads the float value, and then change the commissioon according to the value of the sale. But with this one I need to change the commission according to the catalogue code (A,B,C...), but I can't make the if statement work. Please help anyone!

I'd like to thanks in advance.


 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The '==' operator asks if two objects are the same object -- physically the same chunk of the computer's memory. A String read from the keyboard and a String built into your program code are not going to be the same object -- they're going to be two separate String objects containing the identical list of characters.

The "equals()" method is intended to compare objects to see if they're "the same" in a more colloquial sense: are they equivalent, or interchangeable, even though they actually be physically distinct. you should never compare Strings with "==", because it's that second idea of equivalence that you really care about. Replace all your

catCode == "A"

with

catCode.equals("A")

and you'll have this problem licked. For Strings, there's even an "equalsIgnoreCase()" method which you may find very useful!

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I'll add them for you since you are new and so that you can see the difference.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from what Ernest said, it's uncommon to indent else-if statements like that. The following (not including the fix of course) is used more often:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Apart from what Ernest said, it's uncommon to indent else-if statements like that.


When I saw it, Wouter hadn't put in the CODE tags yet!
 
Alex Manfredi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem fixed!

Thanks guys, a lot!



P.S. I will remember the tags next time.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!