Scanner, Command Line Arguments, and File I/O Flashcards

1
Q

What is the Scanner class used for in Java?

A

The Scanner class in Java is used for reading input, whether it’s from the console or from a file.

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

Can you use the Scanner class to read input other than strings?

A

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.

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

Is the Scanner class a static class?

A

No, the Scanner class is not static. It is an object-oriented class that must be instantiated before it can be used.

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

How does the Scanner class behave like a stream?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does System.out relate to the Scanner class?

A

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.

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

What is an input stream?

A

An input stream is a sequence of data that can be read from a source, such as a file or the console.

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

Is the Scanner class an input stream in Java?

A

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.

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

What does it mean for the Scanner class to parse input into tokens?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the Scanner class automatically convert tokens into different types?

A

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.

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

Can multiple Scanner objects be used in a Java program?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create an input stream using the Scanner class?

A

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);.

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

What is System.in in Java?

A

System.in is an input stream in Java that represents the standard input stream, which is usually the console or command line input.

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

What does it mean to instantiate a new Scanner object?

A

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.

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

Can you create a Scanner object that reads input from a file?

A

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”));.

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

What happens if the source of the stream specified in the Scanner object doesn’t exist?

A

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.

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

What is the hasNext() method in the Scanner class used for?

A

The hasNext() method in the Scanner class is used to check if there is another token available on the input stream.

17
Q

What does the hasNext() method return?

A

The hasNext() method returns a boolean value: true if there is another token available on the input stream, and false otherwise.

18
Q

Are there more specific versions of the hasNext() method in the Scanner class?

A

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.

19
Q

How can the hasNext() method be used in a loop?

A

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
}

20
Q

What happens if the hasNext() method is called on an empty input stream?

A

If the hasNext() method is called on an empty input stream, it will immediately return false.

21
Q

What is the File class in Java used for?

A

The File class in Java is used to represent a file or directory path on a file system.

22
Q

How do you create an instance of the File class?

A

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”);

23
Q

What is the purpose of the filename parameter when creating a new File object?

A

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.

24
Q

How can you use an instance of the File class to read from a file?

A

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);.

25
Q

What happens if the file specified by the File object doesn’t exist or is inaccessible?

A

If the file specified by the File object doesn’t exist or is inaccessible, an exception will be thrown when attempting to read from the file using the Scanner class.

26
Q

What happens when you try to create a Scanner object to read from a file that doesn’t exist?

A

If you try to create a Scanner object to read from a file that doesn’t exist, a FileNotFoundException will be thrown.

27
Q

How can you handle a FileNotFoundException when trying to read from a file?

A

You can handle a FileNotFoundException by enclosing the creation of the Scanner object in a try-catch block. If a FileNotFoundException is caught, you can handle the error appropriately. For example:

java
Copy code
File inputFile = new File(filename);
Scanner in = null;
try {
in = new Scanner(inputFile);
} catch (FileNotFoundException e) {
System.out.println(“Cannot open “ + filename);
System.exit(0);
}

In this example, if a FileNotFoundException is caught, the program prints an error message and exits.

28
Q

What is the purpose of the System.exit(0) statement in the example code?

A

The System.exit(0) statement in the example code is used to terminate the program when a FileNotFoundException is caught. The argument 0 passed to the exit() method indicates that the program exited normally.

29
Q

Why is it important to close input streams when you’re finished with them?

A

It’s important to close input streams when you’re finished with them because it releases any resources associated with the stream, such as file handles or network connections. If you don’t close the stream, the resources may not be released until the program exits, which can cause issues if you’re working with many files or connections.

30
Q

How do you close an input stream in Java?

A

To close an input stream in Java, you can call the close() method on the stream object. For example, to close a Scanner object, you would call in.close(); where in is the name of your Scanner object.

31
Q

Can you close System.in using the close() method?

A

It is not recommended to close System.in using the close() method because it can have unexpected consequences. Closing System.in could make it impossible for the user to enter more input, and it can also cause issues with other parts of the program that rely on System.in. Instead, it’s better to leave System.in open and let the operating system handle it.

32
Q

How is the PrintWriter class different from the Scanner class?

A

The PrintWriter class and Scanner class are both streams used for input and output, but they have different methods for reading and writing data. The Scanner class is used for reading data from a stream, while the PrintWriter class is used for writing data to a stream.

33
Q

Why is it important to close a PrintWriter object after use?

A

It’s important to close a PrintWriter object after use to ensure that any buffered data is flushed to the file and that any resources associated with the stream are released. If you don’t close the PrintWriter object, you risk losing data that is still in the buffer and may cause issues if you’re working with many files. You can close a PrintWriter object by calling its close() method.

34
Q

What is the difference between the print and println methods in the PrintWriter class?

A

The print method in the PrintWriter class prints a value to the output stream without a new line character at the end. The println method, on the other hand, prints a value to the output stream with a new line character at the end.

35
Q

Can the print and println methods be used for printing different types of data?

A

Yes, both the print and println methods in the PrintWriter class can be used to print different types of data, including int, double, String, and more.