This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello. I am new to java. I am having difficulty with if else statements. I want it so that when the imputer types astra, Vauxhall should appear and if he types Fiesta, then Ford should appear. This is done in case 2.
All thats being printed to screen when I choose option 2 and type astra is the word Nissan. I belive I have got a simple error can you show me how to do itt properly. Thx
The concept to take note of here is that the == operator performs an identity comparison, to determine whether two references refer to the same object, while the equals method is typically written to determine whether two objects have the same value.
think of your string variables (or any variable that refers to an Object) as pieces of mail. there's a letter inside an envelope. the envelope has an address on it. when you use "==", you are saying "am i sending these two letters to the same place?". sometimes this is useful, but not very often. it's almost never what you want. when you use the ".equals" method, you are saying "are the letters in both envelopes the same?" that's what you want here.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
In fact, you might want to use "equalsIgnoreCase()" instead. That way you don't have to worry whether the user types "astra" or "Astra" or "ASTRA". It simply does a case-insensitive comparison. You could also call "trim()" on the input string in case they added trailing spaces.