User Input Flashcards

1
Q

What does the built in Scanner class do?

A

It allows us to create a Scanner object which can be used to get and store input(s).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is the Scanner class imported?

A

At the top of our code we import the class with this line of code:
import java.util.Scanner;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the line of code that creates a new object of the Scanner named input from type System.in

A

Scanner input = new Scanner(System.in);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does using the argument System.in allow in the following code?
Scanner input = new Scanner(System.in);

A

By using System.in as an argument to create a Scanner type object, input can be read from the standard input channel, which for us is the terminal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What type of value does the nextLine() Scanner input method expect?

A

String. This method reads input until the end of a line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What type of value does the next() Scanner input method expect?

A

String. This method reads input until it reaches a space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What would be the Scanner method for the following data types?
byte
long
double

A

nextByte()
nextLong()
nextDouble()
The Scanner class contains several methods, with directly related names, to read primitive data type input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if an unexpected input type is entered into a scanner method?

A

A InputMismatchException will be thrown.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is it recommended that we close the Scanner when it is no longer being used?

A

It allows java to reclaim the memory used by the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax used to close the scanner object named keyboard?

A

keyboard.close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the syntax to read a line of text input from the Scanner object keyboard and save it into the String variable userId?

A

String Userid = keyboard.nextLine()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Scanner is the easiest and commonly used class built into java that can be used to read input. What are a couple others?

A

BufferedReader
InputStreamReader

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we check to see if the input data type we get is the correct type?

A

By using input type checking methods in if statements. These return a boolean value indicating if the input matches the expected value type.
scanner.hasNextInt()
scanner.hasNextDouble()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What would the syntax be to check if the input from the user the Scanner object keyboard receives is of the int data type?

A

keyboard.hasNextInt()
This returns a Boolean and can be used in loops or if statements to ask for input again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly