-1

Me very new programmer, I'm making a simple vending machine program, and so far I have two items. I setup a scanner that takes user input and if statements that evaluate to true if a user enters a specific input. When I enter "A" for example it prints both if statements when it should only print one.

Here is my code so far:

import java.util.Scanner;

public class Testing {

    public static void main(String args[]) {
    Scanner scnr = new Scanner(System.in);

    double milkPrice = 2.00;
    double sodaPrice = 2.25;

    System.out.println("Please make a selection:");
    String userResponse = scnr.next();

    //If user input is A or a
    if (userResponse.equals("A") ||
            userResponse.equals("a"))
            {
        System.out.printf("%s", "You have bought Milk for  "); System.out.printf("%1.2f %n", milkPrice);
            }

    System.out.println("Please make a selection:");
    //If user input is B or b
    if  (userResponse.equals("B") ||
            userResponse.equals("b"));
    {
        System.out.printf("%s", "You have bought Soda for "); System.out.printf("%1.2f %n", sodaPrice);
    }
        scnr.close();

    }
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
TeemoMain
  • 105
  • 1
  • 1
  • 8

2 Answers2

0

Your if statement has a semicolon where there should not be one:

if  (userResponse.equals("B") ||
    userResponse.equals("b"));

A typical if-statement, without brackets, will execute just the single next line if the condition evaluates to true. However, your single next line is an empty line denoted by ;. Then, the next block of text in brackets is executed no matter what.

Kon
  • 10,702
  • 6
  • 41
  • 58
0

You have an erroneous semicolon at the end of your second if statement.

Take out the semicolon and let the if statement condition flow into the response.

Bango
  • 971
  • 6
  • 18