Lecture1c- Scanner Flashcards

1
Q

What is System.in in Java?

A

A standard input stream used to read input from the console.
An instance of InputStream, automatically instantiated by Java.
Typically wrapped by a Scanner object for easier input handling.

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

What is the Scanner class in Java?

A

A text parser that reads numbers, words, or phrases from an input source.
Helps create interactive programs.
Defined in java.util.Scanner and must be imported.

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

How do you create a Scanner object?

A

import java.util.Scanner;
Scanner scan = new Scanner(System.in);

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

What are some common Scanner methods?

A

next() → Reads next token as a String
nextLine() → Reads entire line as a String
nextInt() → Reads an int
nextDouble() → Reads a double
hasNext() → Checks if another token exists

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

How do you read different data types using Scanner?

A

int num = scan.nextInt();
double decimal = scan.nextDouble();
String word = scan.next();
String line = scan.nextLine();

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

What are some useful Scanner validation methods?

A

hasNext() → Checks if there is another token.
hasNextLine() → Checks if another line exists.
hasNextInt() → Checks if the next token is an int.
hasNextDouble() → Checks if the next token is a double.

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

What is the difference between print() and println()?

A

print() → Prints output but stays on the same line.
println() → Prints output and moves to the next line.

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

What does this Java program do?

import java.util.Scanner;

public class TestScanner {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(“Enter a number: “);
double value = scan.nextDouble();
System.out.println(“Entered “ + value);
}
}

A

Prompts the user to enter a number.
Reads the number as a double.
Displays the entered number.

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

What is an example of using nextLine()?

A

Scanner scan = new Scanner(System.in);
System.out.println(“Enter a sentence:”);
String message = scan.nextLine();
System.out.println(“You entered: “ + message);

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