0
0
Javaprogramming~5 mins

Reading string input in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What class in Java is commonly used to read string input from the user?
The <strong>Scanner</strong> class is commonly used to read string input from the user in Java.
Click to reveal answer
beginner
How do you create a Scanner object to read input from the keyboard?
You create it with: Scanner scanner = new Scanner(System.in); This connects the Scanner to the keyboard input.
Click to reveal answer
beginner
Which Scanner method reads a whole line of text including spaces?
The method nextLine() reads the entire line of input including spaces until the user presses Enter.
Click to reveal answer
beginner
What happens if you use next() instead of nextLine() to read a string?
next() reads only the next word (up to the first space), not the whole line.
Click to reveal answer
intermediate
Why should you close the Scanner object after use?
Closing the Scanner with scanner.close(); frees system resources and avoids resource leaks.
Click to reveal answer
Which method reads a full line of user input including spaces?
Ainput()
Bnext()
CnextLine()
DreadLine()
How do you create a Scanner to read from the keyboard?
AScanner scanner = new Scanner(System.in);
BScanner scanner = new Scanner(System.out);
CScanner scanner = new Scanner(System.err);
DScanner scanner = new Scanner(System.in.read());
What does next() method read from input?
AThe next word (up to space)
BThe entire line
CThe next character
DNothing, it throws error
Why is it important to close a Scanner object?
ATo speed up input reading
BTo free system resources
CTo clear the input buffer
DIt is not important
Which import statement is needed to use Scanner?
Aimport java.util.Input;
Bimport java.io.Scanner;
Cimport java.lang.Scanner;
Dimport java.util.Scanner;
Explain how to read a full line of string input from the user in Java.
Think about connecting Scanner to keyboard and reading the entire line.
You got /3 concepts.
    What is the difference between next() and nextLine() methods in Scanner?
    Consider how spaces affect what each method reads.
    You got /4 concepts.