1

I am trying to read user input and it seems like its not setting the variable to the input.

For example:

System.out.println("Would you like Syrup?(Y/N)");
   String wantSyrup = keyboard.next();
   char syrupWant = wantSyrup.charAt(0);

   switch (syrupWant){

   case 'y':
       System.out.println("Which syrup would you like?");
        System.out.println("Hot fudge, Chocolate, Caramel, Strawberry");
         syrupChoice = keyboard.nextLine();
        keyboard.nextLine();
        System.out.println("You entered: "+syrupChoice);

     if((syrupChoice.equalsIgnoreCase(hotFudge))
                || (syrupChoice.equalsIgnoreCase(chocolateSyrup))
                || (syrupChoice.equalsIgnoreCase(caramel))
                || (syrupChoice.equalsIgnoreCase(strawberrySyrup)))
            {
                System.out.println(syrupChoice);
            }else{
                System.out.println("Not a valid choice. You now have Hot Fudge as your syrup.");
                syrupChoice = hotFudge;
            }
            break;
        default: break;
    }

The Console is coming back with this when i enter any type of syrup..

Would you like Syrup?(Y/N)
y
Which syrup would you like?
Hot fudge, Chocolate, Caramel, Strawberry
caramel
You entered: 
Not a valid choice. You now have Hot Fudge as your syrup.

Entire Code (not incuding sundae class)


import java.util.Scanner;
import java.text.*;

public class SundaeLab {

    public static void main(String[] args){

    Sundae userSundae = new Sundae();
    Scanner keyboard = new Scanner(System.in);
     String hotFudge = "Hot Fudge";
     String chocolateSyrup = "chocolate";
     String caramel = "caramel";
     String strawberrySyrup = "strawberry";
     String syrupChoice = "";
     String vanilla = "vanilla";
     String peanutButter = "peanut butter";
     String chocolate = "chocolate";
     String coconut = "coconut";
     String cookieDough = "cookie Dough";
     String coffee = "coffee";
     String strawberry = "strawberry";
     String butterPecan = "butter pecan";
     double subtotal;
     double tax;
     final double SUFFOLK_SALES_TAX = .08625;
     double costOfSundae;
     DecimalFormat df = new DecimalFormat("#.##");



    System.out.println("What flavor of Ice Cream would you like?");
    System.out.println("Your choices are: Vanilla, Peanut Butter, Chocolate, Coconut, Cookie Dough,");
    System.out.println(" Coffee, Strawberry, Butter Pecan.");

    String flavor = keyboard.nextLine();
    System.out.println("You entered: "+flavor);
    System.out.println("How many scoops of ice cream would you like?");
    System.out.println("One scoop is $1.79. Additional scoops are $1.00 Each.");
    System.out.println("You can have 6 scoops max");
    int numberOfScoops = keyboard.nextInt();
    System.out.println("You entered: "+numberOfScoops);
    String dToppings = "";

    //System.out.UserSundae.getFlavor();
    if (flavor.equalsIgnoreCase("vanilla")){
        userSundae.setFlavor(vanilla);
    }
    if (flavor.equalsIgnoreCase("peanut butter")){
        userSundae.setFlavor(peanutButter);
    }
    if (flavor.equalsIgnoreCase("chocolate")){
        userSundae.setFlavor(chocolate);
    }
    if (flavor.equalsIgnoreCase("coconut")){
        userSundae.setFlavor(coconut);
    }
    if (flavor.equalsIgnoreCase("cookie dough")){
        userSundae.setFlavor(cookieDough);
    }
    if (flavor.equalsIgnoreCase("coffee")){
        userSundae.setFlavor(coffee);
    }
    if (flavor.equalsIgnoreCase("strawberry")){
        userSundae.setFlavor(strawberry);
    }
    if (flavor.equalsIgnoreCase("butter pecan")){
        userSundae.setFlavor(butterPecan);
        }

    if (!flavor.equalsIgnoreCase(vanilla) && !flavor.equalsIgnoreCase(coffee) && !flavor.equalsIgnoreCase(peanutButter)&&
            !flavor.equalsIgnoreCase(chocolate)&& !flavor.equalsIgnoreCase(coconut) && !flavor.equalsIgnoreCase(cookieDough)&& 
            !flavor.equalsIgnoreCase(strawberry)&& !flavor.equalsIgnoreCase(butterPecan)){
        userSundae.setFlavor(vanilla);
        flavor = vanilla;
        System.out.println("We Changed your flavor to Vanilla");
        }

    if ( numberOfScoops <= 6 && numberOfScoops > 0){}
    else{
        userSundae.Sundae();
        System.out.println("We Changed your scoop amount to: 2");
        numberOfScoops = 2;
    }
    if (numberOfScoops>1){
    subtotal = (numberOfScoops - 1)*1.00 + 1.79;
    }
    else{
        subtotal = 1.79;
    }
    System.out.println("Would you like Whipped Cream?(Y/N)");
    String wantsWhipped = keyboard.next();
    char whippedWants = wantsWhipped.charAt(0); 

    if ( whippedWants == 'y'){
        String dTopppings = dToppings  + "Whipped Cream ";
        System.out.println("Whipped cream added");
    }

   System.out.println("Would you like Syrup?(Y/N)");
   String wantSyrup = keyboard.next();
   char syrupWant = wantSyrup.charAt(0);

   switch (syrupWant){

   case 'y':
       System.out.println("Which syrup would you like?");
        System.out.println("Hot fudge, Chocolate, Caramel, Strawberry");
         syrupChoice = keyboard.nextLine();
        keyboard.nextLine();
        System.out.println("You entered: "+syrupChoice);

          if((syrupChoice.equalsIgnoreCase(hotFudge))
                    || (syrupChoice.equalsIgnoreCase(chocolateSyrup))
                    || (syrupChoice.equalsIgnoreCase(caramel))
                    || (syrupChoice.equalsIgnoreCase(strawberrySyrup)))
                {
                    System.out.println(syrupChoice);
                }else{
                    System.out.println("Not a valid choice. You now have Hot Fudge as your syrup.");
                    syrupChoice = hotFudge;
                }
                break;
            default: break;
        }

    System.out.println("Our Deluxe toppings are an extra cost of .75 cents each.");
    System.out.println("Would you like to see them? (Y/N)");
    String deluxeList = keyboard.nextLine();
    if(deluxeList.equalsIgnoreCase("y")){

    System.out.println("Would you like M&M's(Y/N)");
    String mM = keyboard.nextLine();
    if (mM.equalsIgnoreCase("y")){
    dToppings = dToppings + "M&M's ";
    subtotal = subtotal + .75;
    System.out.println("M&M's Added");
}
    System.out.println("Would you like Crushed Oreos(Y/N)");
    String oreos = keyboard.nextLine();
    if (oreos.equalsIgnoreCase("y")){
    dToppings = dToppings + "Crushed Oreos ";
    subtotal = subtotal + .75;
    System.out.println("Crushed Oreo's Added");

    }
    System.out.println("Would you like Reeses pieces(Y/N)");
    String reeses = keyboard.nextLine();
    if (reeses.equalsIgnoreCase("y")){
    dToppings = dToppings + "Reese's Pieces ";
    subtotal = subtotal + .75;
    System.out.println("Reese's Peices Added");

    }
    System.out.println("Would you like Brownie Crunchies(Y/N)");
    String crunchies = keyboard.nextLine();
    if (crunchies.equalsIgnoreCase("y")){
    dToppings = dToppings + "Brownie Crunchies  ";
    subtotal = subtotal + .75;
    System.out.println("Brownie Crunchies Added");

    }
    System.out.println("Would you like Mint Chocolate Chips(Y/N)");
    String chips = keyboard.nextLine();
    if (chips.equalsIgnoreCase("y")){
    dToppings = dToppings + "Mint Chocolate Chips ";
    subtotal = subtotal + .75;
    System.out.println("Mint Chocolate Chips Added");

    }   
    }
    tax = subtotal * SUFFOLK_SALES_TAX;
    costOfSundae = tax + subtotal;

    System.out.println("You got "+numberOfScoops+" Scoops of "+flavor);
    System.out.println("Your syrup is: "+syrupChoice);
    System.out.println("Your toppings are: "+dToppings);
    System.out.println("Your subtotal is: "+ df.format(subtotal));
    System.out.println("the tax is: "+df.format(tax));
    System.out.println("Your total is: "+df.format(costOfSundae));
    }
}
TJ Zimmardi
  • 35
  • 2
  • 10

2 Answers2

1

Change

syrupChoice = keyboard.nextLine();

for

syrupChoice = keyboard.next();

As you can see with this output :

You entered: 

even if you typed caramel, it is obviously not what is stored in syrupChoice.

See What's the difference between next() and nextLine() methods from Scanner class? for the difference between both methods.

Community
  • 1
  • 1
Jean-François Savard
  • 19,624
  • 6
  • 42
  • 70
  • This worked, although now it isnt reading Hot Fudge as two words. I believe this is because next() is space delimited. When i input "hot fudge" it comes up with "You entered: Hot" and defaults it. – TJ Zimmardi Apr 20 '15 at 02:09
  • @TJZimmardi Yep `next` is `\\s` delimited. You could use `nextLine` and simply call it before reading the option to read the last carriage return. – Jean-François Savard Apr 20 '15 at 02:11
  • Im sorry, but i'm not sure what you mean. Would you care to elaborate a bit more? – TJ Zimmardi Apr 20 '15 at 02:16
  • @TJZimmardi To resume, use your old code, but call `nextLine` two times (one time before where you read the choice). – Jean-François Savard Apr 20 '15 at 02:17
1

Add a keyboard.nextLine() after the keyboard.next(). This will read in the newline that is still hanging out in the buffer after you type the Y or N response.