0

The or statement in the first line of my if statement is not working as i would expect it to, any suggestions?

if(answer == "deposit" || "Deposit"){
        System.out.println("How much would you like to deposit?");
        final int deposit = console.readInt();
        account.deposit(deposit);
        System.out.println( "This leaves you with $" + formatter.format(account.getBalance()) + " in your account");
    }else if(answer == "Check Balance"){
        System.out.println( "You have $" + formatter.format(account.getBalance()) + " in your account");
    }else if(answer == "Withdraw"){
        System.out.println("How much would you like to take out?");
        final int with = console.readInt();
        account.withdraw(with);
        System.out.println( "This leaves you with $" + formatter.format(account.getBalance()) + " in your account");
    }else{
        System.out.println("You didn't enter a correct term, please try again.");
    }
sirnomnomz
  • 623
  • 1
  • 7
  • 20
  • @Reimeus Sure, that's obviously part of it, but there's the whole trying to use two values and one comparison operator thing also. – rgettman Jan 22 '14 at 18:04

6 Answers6

3

First, logical-OR conditions don't work this way in Java like they do in English. You must write it out explicitly. Second, don't use == to compare String values. Use equals() for all String value comparisons.

if ("deposit".equals(answer) || "Deposit".equals(answer))

Or you can use equalsIgnoreCase().

if ("deposit".equalsIgnoreCase(answer))
Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

Use the method 'equals' for string comparison. E.g. answer.equals("deposit").

Paul Facklam
  • 1,623
  • 12
  • 16
0

Use if (answer.equals("deposit") || answer.equals("Deposit")) {

and }else if(answer.equals("Check Balance")){

Sujith Surendranathan
  • 2,569
  • 17
  • 21
0

Testing if strings are equal in Java should not be done with ==, but with:

if ( answer.equals("Check Balance") ) 

With == you compare pointers of String objects, but answer and "Check Balance" are different object - second one is created during test so even if they have equal value, they are not identical. equals check only object's content.

hsz
  • 148,279
  • 62
  • 259
  • 315
0

For String you need to use String.equals method

if("deposit".equals(answer)){
Vaandu
  • 4,857
  • 12
  • 49
  • 75
0

Change

if(answer == "deposit" || "Deposit"){

as

if(answer.equalsIgnoreCase("deposit")){

or

if (answer.equals("deposit") || answer.equals("Deposit")) {

Reason

equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects!. That means, the contents of the objects. Whereas the == operator is expected to check the actual object instances are same or not.

Example

Consider two different reference variables str1 and str2

str1 = new String("abc");
str2 = new String("abc");

if you use the equals()

System.out.println((str1.equals(str2))?"TRUE":"FALSE");

You will get the output as TRUE

if you use ==

 System.out.println((str1==str2)?"TRUE":"FALSE");

Now you will get the FALSE as output because both str1 and str2 are pointing to two different objects even though both of them share the same string content. It is because of new String() everytime a new object is created.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55