Spider-Man
Spider-Man

Reputation: 1

Array errors I'm facing using Scanner

I'm working on an assignment. Parallel arrays are required... I need help with a couple of things, well, at least three things.

I'll appreciate any help at this point!

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.25, 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();

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



  System.out.println("Weekday     Drink       Original-Price     Discount-Price");
  System.out.println("----------------------------------------------------------");
  System.out.println(days[0] + drinks[0] + price[0]); //Print out the list of the desire array when you enter a day in


  System.out.println("The highest price drink is latte at $3.75");


 }
}

Upvotes: 0

Views: 95

Answers (3)

Nicholas K
Nicholas K

Reputation: 15423

Ok here we go...

  • How do I add spaces in the print statement?

    Add spaces as shown below

    System.out.println(days[0] + "  " + drinks[0] + "  " + price[0]);
    
  • Another problem is the "1.0" I clearly have "1.25" for price [0] but why is it printing out "1.0"?

    Not sure what you mean, but if you input 1 it outputs 1.0 so on and so forth

  • Which day of the week do you want...." It stills prints out the information for Monday. How do I code it where if you type in Tuesday

    This is happening because you are storing the input in day and trying to use the index of array days. Just print out the day variable, you dont need the array days.

     System.out.println(day + "  " + drinks[0] + "  " + price[0]);
    

Upvotes: 1

MWB
MWB

Reputation: 1879

Parallel arrays is not a thing, but you could of course make a new class, called e.g., Drink.

public class Drink {
    String drink;
    double price;

    public Drink(String drink) {
        this.drink = drink;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDrink() {
        return drink;
    }

    public double getPrice() {
        return price;
    }
}

This way, you can use the constructor to create the drink and the setter to set the price, after you got the user input. You could also include a field for discount (or discount of the day) or something.

In your scenario, not sure where the days are for...

Upvotes: 0

user1907859
user1907859

Reputation: 574

First of all you have an extra semicolon after the price variable that should result in a compilation error:

double [] price; = {1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 3.75};

Secondly you never use the values you instantiate the array with, you assign new values as you run the program. So if you answer the first question - "What is the price of a Soda?" - with 1, then the end result will be 1.0.

Thirdly, to add the required space, just add it when you print out the result:

System.out.println(days[0] + "  " + drinks[0] + "  " + price[0]);

Upvotes: 1

Related Questions