File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Simple Problem (if else statments) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Problem (if else statments)" Watch "Simple Problem (if else statments)" New topic

Simple Problem (if else statments)

macca Mason

Joined: Nov 26, 2003
Posts: 13
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

Joined: Mar 26, 2004
Posts: 83
use string1.equals( string2 ) to check for the input instead of string1 == string2
check out some tutorials about strings.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Checkout the Specification of String class at
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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.
I agree. Here's the link:
subject: Simple Problem (if else statments)
It's not a secret anymore!