2

I am working on an assignment with arrays. I have to use parallel arrays, however, I am having difficulties with the desired output I want the code to produce. The trouble is at the end of the code where it asks to enter in the day. I tried doing String [i] days.nextLine, String days next.Line, but it does not work.

I will provide a picture to clarify what I am trying to do. Warning: the picture is blurry. I'm doing good so far, it's the last three parts that present a challenge. I will add the discount portion later on. Thanks.

  System.out.println("Weekday     Drink       Original-Price     Discount-Price");
  System.out.println("------------------------------------------------------");
  System.out.println(day +  drink  + price      +          "IDK"      );

enter image description here

import java.util.Scanner;

public class Cafeteria 
{
    public static void main (String [] args)
    {
        String [] days = {"Monday: ", "Tuesday: ", "Wednesday: ", "Thursday: ", "Friday: ", "Saturday: ", "Sunday: "}; 
        String [] drinks = {"Soda", "Sweet Tea", "Lemonade", "Frozen   Lemonade", "Coffee-Hot", "Coffee-Iced", "Latte"}; 
        double [] price = {1.00, 1.50, 1.75, 2.00, 2.25, 2.50, 3.75};

        for ( int i = 0; i < days.length; i++)
        {
        }

        Scanner scan = new Scanner(System.in);
        System.out.println("What is the price of a Soda? ");
        price [0] = scan.nextDouble();

        System.out.println("What is the price of a Sweet Tea? ");
        price [1] = scan.nextDouble();

        System.out.println("What is the price of a Lemonade? ");
        price [2] = scan.nextDouble();

        System.out.println("What is the price of a Frozen Lemonade? ");
        price [3] = scan.nextDouble();

        System.out.println("What is the price of a Coffee-Hot? ");
        price [4] = scan.nextDouble();

        System.out.println("What is the price of a Coffee-Iced? ");
        price [5] = scan.nextDouble();

        System.out.println("What is the price of a Latte? ");
        price [6] = scan.nextDouble();
        System.out.println();

        System.out.println("Which day of the week do you want the discounted drink price for?");

        System.out.println("Weekday    Drink   Original-Price  Discount-Price");
        System.out.println("-------------------------------------------------");
    }
}

1 Answers1

0

If I understand correctly, you are trying to scan a string but your code isn't working. That would be because when you try to scan for a string using nextLine(), it is scanning the left over /n (line-break) from the previous nextDouble() call. So just do something like the following:

System.out.println("What is the price of a Latte? ");
price[6] = scan.nextDouble(); // nextDouble() only returns the double value and leaves the '/n' character still in the buffer

scan.nextLine(); // Flushes out the left over '/n'
System.out.println("Which day of the week do you want the discounted drink price for?");
String day = scan.nextLine();

This will now get the string you input and assign it to the variable day.

0xCursor
  • 2,242
  • 4
  • 15
  • 33