Scanner, Command Line Arguments, and File I/O Flashcards
What is the Scanner class used for in Java?
The Scanner class in Java is used for reading input, whether it’s from the console or from a file.
Can you use the Scanner class to read input other than strings?
Yes, the Scanner class has many non-static methods that can be used to read input of various types, including integers, doubles, and other primitive types.
Is the Scanner class a static class?
No, the Scanner class is not static. It is an object-oriented class that must be instantiated before it can be used.
How does the Scanner class behave like a stream?
The Scanner class behaves like a stream because it allows you to read input sequentially, one piece at a time, similar to how data is read from a stream. Additionally, it can be used to read input from various sources, just like a stream can be used to read data from various sources.
How does System.out relate to the Scanner class?
System.out is also a stream in Java, but it is used for output rather than input. The println method can be used to write data to the terminal, which is similar to how the Scanner class can be used to read input from the terminal.
What is an input stream?
An input stream is a sequence of data that can be read from a source, such as a file or the console.
Is the Scanner class an input stream in Java?
Yes, the Scanner class is an input stream in Java. It can be used to parse files and command line input into tokens separated by spaces, and it can automatically convert tokens on the stream into different types.
What does it mean for the Scanner class to parse input into tokens?
When input is parsed into tokens, it means that the input is broken up into individual pieces, or tokens, based on a specified delimiter. In the case of the Scanner class, the default delimiter is whitespace.
How does the Scanner class automatically convert tokens into different types?
The Scanner class has methods like nextInt() and nextDouble() that can be used to automatically convert the next token on the input stream into a specific type. If the token cannot be converted to the specified type, an exception is thrown.
Can multiple Scanner objects be used in a Java program?
Yes, multiple Scanner objects can be used in a Java program. Each Scanner object can be associated with a different input stream, such as a file or the console, allowing the program to read input from multiple sources.
How do you create an input stream using the Scanner class?
To create an input stream using the Scanner class, you first instantiate a new Scanner object and pass in a source of the stream as a parameter. For example: Scanner input = new Scanner(System.in);.
What is System.in in Java?
System.in is an input stream in Java that represents the standard input stream, which is usually the console or command line input.
What does it mean to instantiate a new Scanner object?
To instantiate a new Scanner object means to create a new instance of the Scanner class that can be used to read input from a specific source, such as the console or a file.
Can you create a Scanner object that reads input from a file?
Yes, you can create a Scanner object that reads input from a file by passing in a File object as a parameter when instantiating the Scanner object. For example: Scanner input = new Scanner(new File(“input.txt”));.
What happens if the source of the stream specified in the Scanner object doesn’t exist?
If the source of the stream specified in the Scanner object doesn’t exist, an exception will be thrown when the program tries to read input from the stream.