0

When I enter my value for the String 'decision' as 'Yes' the if statement then acts as if the value of decision is anything but 'Yes' by executing the line System.out.println("Please continue shopping"); What can I do to fix this? Here is the class the problem is in:

 //paying for this instance of a basket
public void payBasket(double cash){
    String decision;
    double balance;
    System.out.println("You have £" +cash);
    decision = null;
    if(cash > endTotal)
    {System.out.println("You have enough money to pay for this basket");
    }
    else 
    {System.out.println("You need more money before you can pay for these items");};
    BufferedReader reader;
    reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Would you like to pay for this basket?");
    try{
     decision = reader.readLine();
    }
    catch (IOException ioe){
        System.out.println("an unecpected error");
    }

    if(decision == "Yes"){balance = endTotal - cash;
        System.out.println("Your current balance is " + balance);}
        else {System.out.println("Please continue shopping");
        }
}
RMURIE200
  • 3
  • 1

3 Answers3

1

To compare String objects, use equals(String myValue) method, not ==

k4sia
  • 414
  • 1
  • 6
  • 18
1

To compare objects in java use .equals() method instead of "==" operator

change

if(decision == "Yes")

to

if("Yes".equals(decision))

if you want to ignore case use .equalsIgnoreCase() method

 if("Yes".equalsIgnoreCase(decision))
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

here ->if(decision == "Yes")

use

if(descision.equals("Yes"));
Nambi
  • 11,944
  • 3
  • 37
  • 49