POPCORN HACKS
Popcorn Hack 1.1
y is 20 because in line 2 x is reassigned to 20 then in line 3 the value of x is copied into y making y = 20
Popcorn Hack 1.2
If you write int count = "hello"; you get a compile-time error because in java the type of the value must match the variable’s declared type.
Popcorn Hack 2.1
If a user types “twenty” when nextInt() expects a number a java.util.InputMismatchException is thrown because nextInt() cannot parse non-numeric text
Popcorn Hack 2.2
If a user enters “San Diego” when next() is called next() stores only “San” because it reads until whitespace and “Diego” remains in the buffer.
Popcorn Hack 3
OUTPUT AT THE BOTTOM OF CODE CELL
import java.util.Scanner;
public class InputLesson {
public static void main(String[] args) {
System.out.println("=== User Information Program ===\n");
Scanner sc = new Scanner(System.in);
// String input
System.out.print("Enter your full name: ");
String name = sc.nextLine();
// Integer input
System.out.print("Enter your age: ");
int age = sc.nextInt();
// Double input
System.out.print("Enter your GPA: ");
double gpa = sc.nextDouble();
// Display results
System.out.println("\n--- Summary ---");
System.out.println("Name: " + name);
System.out.println("Age: " + age + " (next year: " + (age + 1) + ")");
System.out.println("GPA: " + gpa);
sc.close();
}
}
InputLesson.main(null);
=== User Information Program ===
Enter your full name: Enter your age: Enter your GPA:
--- Summary ---
Name: Alice Wonderland
Age: 20 (next year: 21)
GPA: 3.85
HOMEWORK HACKS
HACK 1
import java.util.Scanner;
public class InputLesson {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Prompt for three integers
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.print("Enter third number: ");
int num3 = sc.nextInt();
// Calculate average (use double to avoid integer division)
double average = (num1 + num2 + num3) / 3.0;
// Display result with two decimal places
System.out.printf("Average: %.2f%n", average);
sc.close();
}
}
InputLesson.main(null);
Enter first number: Enter second number: Enter third number: Average: 30.00