You are on page 1of 8

Chapter 5.

Console Input &


Console Output
5.1 Console Input
In Java, you can read user input from the console (also known as standard
input) using classes and methods provided by the ‘java.util’ package. The primary
class for reading console input is ‘java.util.Scanner’. Here's a basic overview of how
to use it:
1. Import the Scanner class:
import java.util.Scanner;
2. Create a Scanner object to read from the console:
Scanner scanner = new Scanner(System.in);
3. Prompt the user for input (optional):
System.out.print("Enter something: ");
4. Read input from the console:
String userInput = scanner.nextLine(); // Reads a line of text
5. Close the Scanner when you're done with it (optional, but a good practice):
Here's a complete example that reads a line of text from the console:
import java.util.Scanner;
public class ConsoleInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
scanner.close();
}
}
This program prompts the user to enter their name, reads the input, and then displays
a greeting using the entered name.

Remember to handle exceptions that might occur when working with user input, such
as ‘InputMismatchException’ when trying to read an integer and the user enters non-
integer data.
Additionally, be cautious when closing the ‘System.in’ input stream, especially in
more complex applications. Closing ‘System.in’ can have unintended consequences,
so it's often better to manage the lifecycle of the ‘Scanner’ object without closing
‘System.in’.

5.2 Console Output


Console output refers to the process of displaying information, messages, or
data on the command line or console window. This is typically done using the
‘System.out’ stream, which is an instance of the ‘PrintStream’ class. You can use
various methods provided by ‘System.out’ to output data to the console.
Here's a detailed explanation of console output in Java:
5.2.1 System.out
System.out is an instance of the ‘PrintStream’ class and represents the standard
output stream, which is usually associated with the console or terminal where your
Java program is running.
5.2.2 Printing Text
You can print text to the console using the ‘System.out.print()’ and
‘System.out.println()’ methods. System.out.print() displays text without a newline
character at the end, so the next output appears on the same line.
System.out.println() displays text with a newline character at the end, which moves
the cursor to the next line after printing.
Example:
System.out.print("Hello, ");
System.out.println("world!");
Output:
Hello world
5.2.3 Printing Variables
You can print the values of variables using System.out.print() or
System.out.println() as well. Example:
int number = 42;
System.out.println("The value of number is: " + number);

Output:
The value of number is: 42
5.2.4 Formatting Output:
You can use ‘System.out.printf()’ to format output with placeholders, similar to
the ‘printf’ function in C. Example:
double price = 19.99;
System.out.printf("The price is %.2f dollars%n", price);
Output:
The price is 19.99 dollars
5.2.5 Escape Sequences:
Java supports escape sequences for special characters. For example, ‘\n’
represents a newline character, ‘\t’ represents a tab character, and ‘\\’ represents a
literal backslash. Example:
System.out.println("Line 1\nLine 2\tTabbed");
Output:
Line 1
Line 2 Tabbed

In summary, console output in Java involves using ‘System.out’ to display information


on the console or terminal. You can print text, variables, format output, and use
escape sequences to control the appearance of the output. It's a fundamental aspect
of Java programming for debugging, providing information to users, and interacting
with the command line environment.
Multiple Choice Questions 5
1. What is the primary purpose of Java's System.out object?
a) Reading keyboard input
b) Writing to the console
c) Managing memory allocation
d) Handling exceptions
Answer: B) Writing to the console
2. Which class is used for console input in Java?
a) Console
b) InputReader
c) Scanner
d) System.in
Answer: C) Scanner
3. How can you read an integer from the console using a Scanner object?
a) scanner.next()
b) scanner.nextInt()
c) scanner.readLine()
d) scanner.readInteger()
Answer: B) scanner.nextInt()
4. Which method is used to print a newline character in Java console output?
a) System.printLine()
b) System.out.print('\n')
c) System.out.println()
d) System.newline()
Answer: C) System.out.println()
5. What is the purpose of the `System.err` stream in Java?
a) Reading standard input
b) Writing error messages to the console
c) Writing normal messages to the console
d) Handling file input/output
Answer: B) Writing error messages to the console
6. Which class is used to format output in Java?
a) Formatter
b) StringFormatter
c) DecimalFormat
d) FormatWriter
Answer: C) DecimalFormat
7. What does the `System.out.format()` method allow you to do?
a) Read user input
b) Format console output using placeholders
c) Clear the console screen
d) Print a blank line
Answer: B) Format console output using placeholders
8. In Java, how do you clear the console screen?
a) System.clear()
b) Console.clear()
c) There is no built-in method to clear the console in Java
d) System.out.println('\u000C')
Answer: C) There is no built-in method to clear the console in Java
9. What is the purpose of the `System.in` stream in Java?
a) Writing data to a file
b) Reading data from a file
c) Writing data to the console
d) Reading data from the keyboard
Answer: D) Reading data from the keyboard
10. How can you read a line of text from the console using a Scanner object?
a) scanner.nextLine()
b) scanner.next()
c) scanner.read()
d) scanner.readln()
Answer: A) scanner.nextLine()
11. Which of the following is a valid way to print the value of a variable `x` to the
console?
a) `System.out.print(x);`
b) `System.out.println(x);`
c) `System.out("x");`
d) `System.print(x);`
Answer: B) `System.out.println(x);`
12. What does the `System.out.flush()` method do?
a) Clears the console screen
b) Flushes the output stream, ensuring data is written immediately
c) Reads user input
d) Prints a newline character
Answer: B) Flushes the output stream, ensuring data is written immediately
13. Which of the following is not a valid escape sequence in Java?
a) `\n`
b) `\t`
c) `\a`
d) `\\`
Answer: C) `\a`
14. What is the purpose of the `System.console()` method?
a) Reading keyboard input
b) Accessing the console object for reading and writing
c) Clearing the console screen
d) Formatting output
Answer: B) Accessing the console object for reading and writing
15. Which class should be used to read password input securely from the console in
Java?
a) Console
b) PasswordReader
c) Scanner
d) InputStreamReader
Answer: A) Console
16. Which method is used to check if a Console object is available for input and output
operations?
a) `System.consoleAvailable()`
b) `Console.isAvailable()`
c) `Console.getInstance()`
d) `System.console()`
Answer: D) `System.console()`
17. What does the `System.out.print()` method do when called with no arguments?
a) Prints a blank line
b) Clears the console screen
c) Waits for user input
d) Prints the string "null"
Answer: D) Prints the string "null"
18. Which of the following is not a valid way to read a character from the console
using a Scanner object?
a) `scanner.nextChar()`
b) `scanner.next().charAt(0)`
c) `scanner.next().charAt(0)`
d) `scanner.next().charAt(0)`
Answer: A) `scanner.nextChar()`
19. Question 19: What is the purpose of the `System.console().readPassword()`
method?
a) Reads a line of text
b) Reads an integer value
c) Reads a password securely
d) Clears the console screen
Answer: C) Reads a password securely
20. Question 20: Which method is used to write a character to the console in Java?
a) `System.out.printChar()`
b) `System.out.writeChar()`
c) `System.out.print()`
d) `System.write()`
Answer: C) `System.out.print()`

You might also like