1

I am writing a program where the user has a choice of 7 movies to buy. The user can choose as many movies up to 7 and than will add up the prices of the movies and than print the total price. I must use arrays in my program. My problem is when the user has already chosen their first movie they are given a choice if they want to buy another movie. I'm confused on how I should code my program by giving the user another choice to choose more than 1 movie. I need help on how I should fix my problem because when I run my program I it won't let me have the choice to choose another movie. Here my code:

import java.util.Scanner;
public class MovieHits {

public static void main(String[] args) 
{
    //Declare Variables
    Scanner keyboard = new Scanner(System.in);
    int userChoice = 0;
    String choice;
    int priceTotal = 0;
    int [] number = {1, 2, 3, 4, 5, 6, 7};

    String [] Movie = new String [7];
    int [] movieCost ={ 5, 4, 3, 6, 4, 4, 3}; 

    Movie [0] = "Iron Man";
    Movie [1] = "Transformers";
    Movie [2] = "Godzilla";
    Movie [3] = "Fast and Furious";
    Movie [4] = "Captain America";
    Movie [5] = "X Men";
    Movie [6] = "Rio";

    //Welcome the user
    System.out.println("Hello, Welcome to TC Movies OnDemand.");

    //Display the listed movies so the user can know with movie they want to watch
    System.out.println("\nChoose which movie you want to watch: ");
    for ( int index = 0; index < 7; index = index + 1 ) 
    {
        System.out.println(number[index]+ ": " +  Movie[index]); 
        System.out.println("\t" + "Price: $" + movieCost[index]);
    }


    //Switch Statement to give user a menu to choose a movie
    userChoice = keyboard.nextInt();

    switch (userChoice)
    {
    case 1: 
        System.out.println("The movie you have chosen is " + Movie[0] + "\nPrice is: " + "$" + movieCost[0]);
        break;

    case 2:
        System.out.println("The movie you have chosen is " + Movie[1] + "\nPrice is: " + "$" + movieCost[1]);
        break;

    case 3:
        System.out.println("The movie you have chosen is " + Movie[2] + "\nPrice is: " + "$" +  movieCost[2]);
        break;

    case 4:
        System.out.println("The movie you have chosen is " + Movie[3] + "\nPrice is: " + "$" + movieCost[3]);
        break;

    case 5:
        System.out.println("The movie you have chosen is " + Movie[4] + "\nPrice is: " + "$" + movieCost[4]);
        break;

    case 6:
        System.out.println("The movie you have chosen is " + Movie[5] + "\nPrice is: " + "$" + movieCost[5]);
        break;

    case 7:
        System.out.println("The movie you have chosen is " + Movie[6] + "\nPrice is: " + "$" + movieCost[6]);
        break;
    default:
        System.out.println("I'm Sorry you did not chose a movie.");
        break;
    }


    //Tell the user if they want to get another movie
    System.out.println("Do you want to add another movie. Enter Yes or No");
    choice = keyboard.next();

    do 
    {
        priceTotal = movieCost[userChoice];

    }
    while (choice.equalsIgnoreCase("Yes"));
    {

    }
    //Tell the user the total price 




}

}

TollesonC
  • 9
  • 4

4 Answers4

3

I would clean your code up like this:

String choice = "Yes"; //Set to yes by default.
//Welcome the user
System.out.println("Hello, Welcome to TC Movies OnDemand.");

while(choice.equalsIgnoreCase("Yes")){
//Display the listed movies so the user can know with movie they want to watch
System.out.println("\nChoose which movie you want to watch: ");
for ( int index = 0; index < 7; index = index + 1 ){
    System.out.println(number[index]+ ": " +  Movie[index]); 
    System.out.println("\t" + "Price: $" + movieCost[index]);
}

userChoice = keyboard.nextInt();

try{
    System.out.println("The movie you have chosen is " + Movie[userChoice] + "\nPrice is: " + "$" + movieCost[userChoice]);
}catch(Exception e){
    System.out.println("Sorry, you did not choose a movie.");
}

//Tell the user if they want to get another movie
System.out.println("Do you want to add another movie? Enter Yes or No.");
choice = keyboard.next();

}

This refactors your code, meaning you don't have to have to use a long switch statement for every single movie type you have.

We've used userChoice as the actual index to look for, and the try{}catch{} block means that we still can 'catch' invalid input if the user incorrectly enters a movie to watch.

I originally was using a do-while like you had, but that structure never works for me, so I swapped it for a straight-forward while loop that I'm more familiar with.

Singular1ty
  • 2,577
  • 1
  • 24
  • 39
0
do {
            //Switch Statement to give user a menu to choose a movie
            System.out.println("Choose your movie number:");
            userChoice = keyboard.nextInt();

            switch (userChoice) {
                case 1:
                    System.out.println("The movie you have chosen is " + Movie[0] + "\nPrice is: " + "$" + movieCost[0]);
                    break;

                case 2:
                    System.out.println("The movie you have chosen is " + Movie[1] + "\nPrice is: " + "$" + movieCost[1]);
                    break;

                case 3:
                    System.out.println("The movie you have chosen is " + Movie[2] + "\nPrice is: " + "$" + movieCost[2]);
                    break;

                case 4:
                    System.out.println("The movie you have chosen is " + Movie[3] + "\nPrice is: " + "$" + movieCost[3]);
                    break;

                case 5:
                    System.out.println("The movie you have chosen is " + Movie[4] + "\nPrice is: " + "$" + movieCost[4]);
                    break;

                case 6:
                    System.out.println("The movie you have chosen is " + Movie[5] + "\nPrice is: " + "$" + movieCost[5]);
                    break;

                case 7:
                    System.out.println("The movie you have chosen is " + Movie[6] + "\nPrice is: " + "$" + movieCost[6]);
                    break;
                default:
                    System.out.println("I'm Sorry you did not chose a movie.");
                    break;
            }

            //Tell the user if they want to get another movie
            System.out.println("Do you want to add another movie. Enter Yes or No");
            choice = keyboard.next();

            priceTotal = movieCost[userChoice];

        } while (choice.equalsIgnoreCase("Yes"));

    }
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • 1
    Thanks bro im going try to fix my program and see what i get thanks for the advice. – TollesonC May 04 '14 at 07:03
  • Now i have another problem when it i enter yes for the choice to get another movie it reprints the same test that i got for my first choice – TollesonC May 04 '14 at 07:45
0

You should use the do-while loop as follows:

  • In the do block, put the code that you would like to happen
    • This should be everything between (and including) "Choose the movie" and retrieving user's choice for repetition.
    • This means that all of that code is guaranteed to occur once, and will occur again if while condition is met.
  • The code block for the while condition is above it, and thus is terminated with a semi-colon.
Neurax
  • 3,657
  • 2
  • 13
  • 18
0
You used do-while loop in wrong place in wrong way.Change Your code to this :

  do{
    try{ 
      userChoice = keyboard.nextInt();
    } catch(Execption e){
         System.out.Println("Error in input!");
    }
switch (userChoice)
{
case 1: 
    System.out.println("The movie you have chosen is " + Movie[0] + "\nPrice is: " +   "$" + movieCost[0]);
    break;

case 2:
    System.out.println("The movie you have chosen is " + Movie[1] + "\nPrice is: " + "$" + movieCost[1]);
    break;

case 3:
    System.out.println("The movie you have chosen is " + Movie[2] + "\nPrice is: " + "$" +  movieCost[2]);
    break;

case 4:
    System.out.println("The movie you have chosen is " + Movie[3] + "\nPrice is: " + "$" + movieCost[3]);
    break;

case 5:
    System.out.println("The movie you have chosen is " + Movie[4] + "\nPrice is: " + "$" + movieCost[4]);
    break;

case 6:
    System.out.println("The movie you have chosen is " + Movie[5] + "\nPrice is: " + "$" + movieCost[5]);
    break;

case 7:
    System.out.println("The movie you have chosen is " + Movie[6] + "\nPrice is: " + "$" + movieCost[6]);
    break;
default:
    System.out.println("I'm Sorry you did not chose a movie.");
    break;
}


  //Tell the user if they want to get another movie

     try{ 
        System.out.println("Do you want to add another movie. Enter Yes or No");
        choice = keyboard.nextLine();
    } catch(Execption e){
         System.out.Println("Error in input!");
    }
    priceTotal = movieCost[userChoice];

} while (choice.equalsIgnoreCase("Yes"));
Ashraful Haque
  • 549
  • 2
  • 10