1 Answers
📚 Quick Study Guide: AP CS A Input/Output Essentials
- 🔍 Scanner Class: The primary tool in Java for reading input from various sources, most commonly the keyboard (`System.in`).
- ✍️ Import Statement: To use `Scanner`, you must include `import java.util.Scanner;` at the top of your Java file.
- 🛠️ Object Creation: Instantiate a `Scanner` object like this: `Scanner console = new Scanner(System.in);`.
- 🔢 Reading Data Types:
- `nextInt()`: Reads an integer value.
- `nextDouble()`: Reads a double-precision floating-point value.
- `next()`: Reads a single token (word) delimited by whitespace.
- `nextLine()`: Reads the entire line of text until the end-of-line character.
- ⚠️ Newline Consumption: After using `nextInt()`, `nextDouble()`, or `next()`, a newline character often remains in the input buffer. Calling `nextLine()` immediately after these without an intermediate `nextLine()` can lead to unexpected empty string reads.
- ♻️ Closing Scanner: It's good practice to close the `Scanner` object when no longer needed to release system resources: `console.close();`.
- 🗣️ System.out.println(): Prints the argument to the console and then advances the cursor to the next line.
- ➡️ System.out.print(): Prints the argument to the console without advancing the cursor to the next line.
- ➕ String Concatenation: Use the `+` operator to combine strings with variables or other data types for output (e.g., `"Hello " + name`).
- 📝 Escape Sequences: Special character combinations within strings:
- `\n`: Newline
- `\t`: Tab
- `\"`: Double quote
- `\\`: Backslash
🧠 Practice Quiz: Test Your I/O Knowledge!
1. Which `Scanner` method is used to read an entire line of text, including spaces, until a newline character is encountered?
- A) `next()`
- B) `nextInt()`
- C) `nextLine()`
- D) `nextWord()`
2. What is the correct way to import the `Scanner` class in Java?
- A) `import java.io.Scanner;`
- B) `import java.util.Scanner;`
- C) `import java.lang.Scanner;`
- D) `import Scanner.java.util;`
3. Consider the following code:
Scanner input = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("You are " + age + " years old.");
input.close();
If the user types `20` and presses Enter, what will be the exact output?
- A)
Enter your age: 20You are 20 years old. - B)
Enter your age: 20
You are 20 years old. - C)
Enter your age: You are 20 years old. - D)
Enter your age:
20
You are 20 years old.
4. Which of the following statements correctly creates a `Scanner` object to read input from the keyboard?
- A) `Scanner keyboard = new Scanner();`
- B) `Scanner keyboard = new Scanner(System.out);`
- C) `Scanner keyboard = new Scanner(System.in);`
- D) `Scanner keyboard = new System.in.Scanner();`
5. What is the primary difference between `System.out.print()` and `System.out.println()`?
- A) `print()` outputs to a file, `println()` outputs to the console.
- B) `print()` adds a newline character at the end, `println()` does not.
- C) `println()` adds a newline character at the end, `print()` does not.
- D) `print()` is for strings, `println()` is for numbers.
6. Which escape sequence represents a tab character in a Java string literal?
- A) `\n`
- B) `\r`
- C) `\"`
- D) `\t`
7. What happens if you try to call `input.nextLine()` immediately after `input.nextInt()` without consuming the newline character?
- A) It will correctly read the next line of user input.
- B) It will throw an `InputMismatchException`.
- C) It will read the leftover newline character from the `nextInt()` input, resulting in an empty string.
- D) It will wait indefinitely for new input.
Click to see Answers
1. C) `nextLine()`
2. B) `import java.util.Scanner;`
3. B) Enter your age: 20
You are 20 years old.
4. C) `Scanner keyboard = new Scanner(System.in);`
5. C) `println()` adds a newline character at the end, `print()` does not.
6. D) `\t`
7. C) It will read the leftover newline character from the `nextInt()` input, resulting in an empty string.
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀