INPUT AND OUTPUT Flashcards

1
Q

What is input and output?

A

Input is any information that is needed by a program to
complete its execution. Output is any information that the program must convey to
the user.

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

What are the various forms of input?

A

text, files, voice, gestures, biometric information etc

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

Why do standard java classes have increased complexity when it comes to input?

A
  • Programs must be written to survive bad input by the user. One way to do this is to ensure that only valid input is accepted.
  • Standard Java classes do not ensure that only valid input is accepted.
  • They are designed to be very flexible to support the wide variety of input and output options available now and in the future.
  • This flexibility comes at the cost of increased complexity.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is console input?

A

Console input is any input that is entered in the console window instead of typing it into a field or dialog box that pops up in a window, e.g. when the nextLine method is called, the program waits for the user to enter information

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

What is a user prompt?

A

A line of text that is output to the user that explains what information they should input next.
• User prompting can be done by displaying information in a dialog box, a program frame, or even the console window.

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

What Java I/O packages are required for successful input reception?

A
  • The java.io package that contains most, if not all, of the classes you will need to use.
  • The java.util package also contains some classes that are useful for input and output.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What specific Java I/O classes are required for successful input reception?

A

• java.io.InputStream - stores information about the
connection between an input device and the computer or program.
• java.util.Scanner - used to read the input available from an InputStream object.

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

What are the steps for console-based user input

A
// 1. Create a Scanner using the InputStream available.
Scanner scanner = new Scanner( System.in );
// 2. Don't forget to prompt the user
System.out.print( "Prompt for user input:" );
// 3. Use the Scanner to read a line of text from the user.
String input = scanner.nextLine();
// 4. Now, you can do anything with the input string that you need to.
// Like, output it to the user.
System.out.println( "input = " + input );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an alternative way of getting console based input?

A
// 1. Create a single shared Scanner object for keyboard input.
// This must be done in only one class of your program.
// All keyboard input must be handled through that one class.
private static Scanner scanner = new Scanner                 ( System.in );
NOTE: Do not forget to import the java.util.Scanner class
*See OOP pics doc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the steps to getting integer input?

A
Get a String of characters that is in an integer format, e.g.,
"123".
String input = scanner.nextLine();
Use the Integer class to parse the string of characters into an integer.
int number = Integer.parseInt(input);//converts a String into an int value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What features does the integer class have?

A
  • The Integer class contains conversion methods for changing String data into int values and vice versa. The Integer class is one of several wrapper classes that are defined in the standard Java API. Wrapper classes have class methods for parsing and are also used when you need to store a primitive value as an object.
  • In our case, we needed to convert a String object into an int value. The parseInt method of the Integer class performs this action.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an alternative direct method of getting integer input?

A
• Read the next available input as an int value directly:
int number = scanner.nextInt();
• The Scanner class contains a method named nextInt
that returns the next input data available as an int
value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When are the following exceptions thrown:
InputMismatchException
NumberFormatException.

A
InputMismatchException- This is exception is thrown when the next input after the nextInt method in the scanner class is not a valid integer format
NumberFormatException.- This exception is thrown when the user types letters instead of digits when the parseInt method of the integer class has been declared
How well did you know this?
1
Not at all
2
3
4
5
Perfectly