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.
What is the hasNext() method in the Scanner class used for?
The hasNext() method in the Scanner class is used to check if there is another token available on the input stream.
What does the hasNext() method return?
The hasNext() method returns a boolean value: true if there is another token available on the input stream, and false otherwise.
Are there more specific versions of the hasNext() method in the Scanner class?
Yes, there are more specific versions of the hasNext() method in the Scanner class, such as hasNextInt(), hasNextDouble(), and hasNextBoolean(). These methods return true if the next token on the input stream is of the corresponding type.
How can the hasNext() method be used in a loop?
The hasNext() method can be used in a loop to iterate through all the tokens in the input stream. For example, a while loop can be used to repeatedly call hasNext() and process the next token on the input stream until there are no more tokens left, like this:
arduino
Copy code
while (scanner.hasNext()) {
String nextToken = scanner.next();
// process the next token
}
What happens if the hasNext() method is called on an empty input stream?
If the hasNext() method is called on an empty input stream, it will immediately return false.
What is the File class in Java used for?
The File class in Java is used to represent a file or directory path on a file system.
How do you create an instance of the File class?
To create an instance of the File class, you instantiate a new File object and pass in a String parameter that represents the file or directory path. For example: File inputFile = new File(“input.txt”);
What is the purpose of the filename parameter when creating a new File object?
The filename parameter is a String that contains the path and filename of the file you want to open. It is used to create an instance of the File class that represents the file on the file system.
How can you use an instance of the File class to read from a file?
To read from a file using an instance of the File class, you pass the File object as a parameter when creating an instance of the Scanner class. For example: Scanner input = new Scanner(inputFile);.