The following code is producing this problem (typed with exact characters printed in command prompt, aside from quotations):

"Please input D for a daytime movie or E for an evening movie: This is not a valid choice."

The program will not allow me to input anything following the prompt for input and skips right to the "This is not a valid choice." Whereas it should only display "This is not a valid choice" if neither E or D are inputted.

Here is the code:

import java.util.Scanner;        //needed for scanner class

/**
    This program will act as a user interface for a movie theatre
*/

public class Theatre
{
    public static void main(String[] args) 
    {


    int adultTickets = 0, childTickets = 0;
    double total = 0, payment = 0;
    int movie = 0;
    String timeOfDay = null;
    final double adultEve = 8.25, childEve = 5.78, adultDay = 6.6, childDay = 4.62;

    //Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);

    //Display the movie titles and prices
    System.out.println("\n               Now Playing                         ID");
    System.out.println("\n\n            Burn After Reading                     1");        
    System.out.println("            Journey to the Center of the Earth 3D  2");
    System.out.println("            Tropic Thunder                         3");
    System.out.println("            Lakeview Terrace                       4\n\n");
    System.out.println("                                 Prices");
    System.out.println("         __________________________________________________________\n\n");
    System.out.println("           Evening:                                   Daytime:");
    System.out.println("\n        Adult:     $8.25                           Adult:     $6.60");
    System.out.println("        Children:  $5.78                           Children:  $4.62\n\n");


    //Prompt user for their movie selection
    System.out.print("Please input the ID number of the movie you would like to watch: ");
    movie = keyboard.nextInt();
    if (movie > 4)
        {
        System.out.println("This is not a valid choice");
        System.exit(0);
        }


    //Prompt user for number of each ticket
    System.out.print("How many child tickets do you want? ");
    childTickets = keyboard.nextInt();
    System.out.print("How many adult tickets do you want? ");
    adultTickets = keyboard.nextInt();
    if ((adultTickets + childTickets) > 100)
        {
        System.out.println("You cannot purchase more than 100 tickets");
        System.exit(0);
        }
    else if ((adultTickets + childTickets) < 0)
        {
        System.out.println("You must purchase at least one ticket");
        System.exit(0); 
        }

    //Prompt user for time of day 
    System.out.print("Please input D for a daytime movie or E for an evening movie: ");
    timeOfDay = keyboard.nextLine();



    //calculate total cost
    if (timeOfDay.equalsIgnoreCase("D"))
        {
        total = (adultTickets * 6.60) + (childTickets * 4.62);
        }
    else if (timeOfDay.equalsIgnoreCase("E"))
        {
        total = (adultTickets * 8.25) + (childTickets * 5.78);
        }
    else
        {
        System.out.println("This is not a valid choice.");
        }

    //Display total cost to the user and get payment
    System.out.println("Your total is: " + total);
    System.out.print("Please input your payment: ");
    payment = keyboard.nextDouble();
    if (payment < total)
        System.out.println("You have not provided enough money");





    }
}

Recommended Answers

All 2 Replies

Simple little fix. I'm not sure why, but once you've made a Scanner class object perform the nextInt() method, it doesn't really want to do the nextLine() method. The easiest way would be to change your program to this:

Scanner keyboard = new Scanner(System.in);
Scanner StringReader=new Scanner(System.in);

or what ever you want to call the second Scanner and then this:

System.out.print("Please input D for a daytime movie or E for an evening movie: ");
timeOfDay = StringReader.nextLine();

It should work fine then. Each Scanner can only look for numbers or strings.

Well done, sir. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.