Exercise Exploration: Going to the Movies
This program is intended to plan the budget to go to the movies. However, there are multiple bugs in the program. Work through the tasks below to debug the program and deepen your understanding of how to use the Scanner class to get user input.
Predict
Read through the program carefully. Can you find one or two of the bugs? Predict what the error message(s) might say.
Run
Run the program. What errors do you get?
Investigate
There are 3 bugs in this program. Two of the bugs are caught during compilation (which you discovered when you ran the code in the previous step), and the third bug is a logic error. Focus first on the errors caught during compilation. Answer the questions below in the Investigate.txt file.
Why does each bug occur?
What change can be made to fix each bug? Describe the change and then make the change in the editor. Once you think you have fixed the bug, run the program to make sure the program runs.
Now that you have the program running, you can find the logic error. Run through the program and follow the prompts to input the correct values. What unexpected behavior occurs?
Think back to the note you read in the previous activity (Scanner Buffer Problem). Why do you think this error happens?
What change can be made to fix this logic error? Describe the change and then make the change in the editor. Once you think you have fixed the bug, run the program to make sure the program runs as intended.
Modify:
Complete the below prompts, which enable the user to budget for multiple tickets.
Immediately after asking for the ticket price (and before you ask about the snacks), ask the user how many tickets they would like to buy. Then, create a new variable called totalTicketPrice to store the total ticket price based on the price of one ticket and the number of tickets.
Adjust the total variable so that it uses the total ticket price.
Why the two checks fail
Look at what the Modify step actually said:
create a new variable called totalTicketPrice
Your code calls it totalPrice. Functionally that's identical, but the autograder checks for the exact variable name, so both checks come back false:
• Checking ticket was modified → looks for totalTicketPrice → not found → false
• Checking total was modified → looks for total = totalTicketPrice + snackPrice → not found → false
(The trufalse in the diff column is just expected true glued to your false.)
Rename totalPrice → totalTicketPrice and both checks pass.
Fixed code
import java.util.Scanner;
public class MovieBudget
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double budget = 40.00;
System.out.println("What movie are you going to see?");
String movie = input.nextLine();
System.out.println("How much is the ticket?");
double ticketPrice = input.nextDouble();
input.nextLine(); // clear the newline left in the Scanner buffer
// ---- Modify step: ask for ticket count BEFORE the snack questions ----
System.out.println("How many tickets are you would like to buy?");
int ticketCount = input.nextInt();
input.nextLine(); // clear the newline left in the Scanner buffer
double totalTicketPrice = ticketPrice * ticketCount;
// ----------------------------------------------------------------------
System.out.println("What snack will you get?");
String snack = input.nextLine();
System.out.println("How much is the snack?");
double snackPrice = input.nextDouble();
input.nextLine(); // clear the newline left in the Scanner buffer
// ---- Modify step: total now uses totalTicketPrice ----
double total = totalTicketPrice + snackPrice;
// -----------------------------------------------------
boolean checkBudget = total <= budget;
System.out.printf("Total price for your movie night: $%.2f%n", total);
System.out.println("Budget Check: $");
System.out.println(checkBudget);
input.close();
}
}
Only two things changed from your version: the variable name, and adding input.close().
The Investigate questions
Predict / Run — the two compile-time bugs
Both are "variable used but never declared", which produces:
MovieBudget.java:24: error: cannot find symbol
double total = totalPrice + snackPrice;
^
symbol: variable totalPrice
location: class MovieBudget
and the same message for snackPrice.
Why: In Java a variable must be declared with its type before you can use it. Writing snackPrice = input.nextDouble(); (no double in front) is an assignment to something the compiler has never heard of. Same for the total line referencing a totalPrice/totalTicketPrice that doesn't exist yet.
Fix: Declare before use — double snackPrice = input.nextDouble(); and double totalTicketPrice = ticketPrice * ticketCount; placed before the line that uses it.
The logic error — Scanner buffer
nextDouble() and nextInt() read the number but leave the Enter keypress (\n) sitting in the buffer. The next call to nextLine() sees that leftover \n, treats it as a complete empty line, and returns "" immediately.
What you observe: "What snack will you get?" prints and then appears to be skipped entirely — you never get to type the snack name, snack becomes "", and the program jumps straight to the snack price.
Why: Because nextLine() reads up to and including the next newline, and there's already one waiting.
Fix: Put a bare input.nextLine(); right after every nextDouble() / nextInt() to swallow that leftover newline. You already have all three in place — that's exactly right.
One more note
System.out.println("Budget Check: $"); followed by printing checkBudget on its own line is leftover weirdness from the starter template (a $ in front of true/false doesn't mean anything). It isn't causing your failures, so leave it alone until the checks pass — if your grader compares exact output, editing it could break things. After everything is green, System.out.println("Budget Check: " + checkBudget); is the cleaner version.

浙公网安备 33010602011771号