0

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

  • The first issue when I run the program all the way through. How do I add spaces in the print statement? It comes out like this "MondaySoda1.0"
  • Another problem is the "1.0" I clearly have "1.25" for price [0] but why is it printing out "1.0"?
  • Lastly, if I type in Tuesday where it asks "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, it does not print anything at all.

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");


 }
}
5
  • There isn't anything called Parallel arrays in java. Commented Oct 24, 2018 at 14:30
  • Yeah, I know, but I just call them for this assignment's sake lol. Commented Oct 24, 2018 at 14:42
  • Nobody besides you understands that term. Maybe you rethink how funny it is to willingly confuse the people you are asking for help. In other words: please ask a clear question, and avoid anything that makes it harder to read than necessary. Commented Oct 24, 2018 at 14:50
  • Seriously... I am calling them arrays because my assignment have instruction to use "parallel" arrays. Stating word for word "(You will create 3 Parallel Arrays: Weekday, Drink & Price)." Commented Oct 24, 2018 at 15:05
  • I think in this particular case "parallel arrays" is a term your teacher uses to mean different arrays that are related through a common index. I.e. they all have the same size and for any i, array1[i] relates to array2[i]. I'm not sure if there is a common computer-science term for this. Commented Oct 24, 2018 at 15:40

3 Answers 3

1

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]);
    
Sign up to request clarification or add additional context in comments.

6 Comments

I see, so I would have to follow the same thing for drinks and price. That way if the user enter Tuesday, for example, it will print Tuesday, Lemonade, and the original price.
Well, how do I store the input in drinks?
Same way you did for price, but ideally you'll need to use a loop to store them rather than use the index each time.
Alright, I just redone my code all over again. Do you mind looking over it?
Stackoverflow isn't the right site for that I'm afraid. You can try this site instead.
|
1

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]);

1 Comment

My fault for putting price;
0

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...

1 Comment

I have made a class to store your Drinks. Pretty much the basics of en.wikipedia.org/wiki/Object-oriented_programming

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.