• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Problem (if else statments)

 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Buchli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use string1.equals( string2 ) to check for the input instead of string1 == string2
check out some tutorials about strings.
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checkout the Specification of String class at http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12266
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic