INPUT AND OUTPUT Flashcards
What is input and output?
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.
What are the various forms of input?
text, files, voice, gestures, biometric information etc
Why do standard java classes have increased complexity when it comes to input?
- 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.
What is console input?
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
What is a user prompt?
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.
What Java I/O packages are required for successful input reception?
- 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.
What specific Java I/O classes are required for successful input reception?
• 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.
What are the steps for console-based user input
// 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 );
What is an alternative way of getting console based input?
// 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
What are the steps to getting integer input?
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
What features does the integer class have?
- 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.
What is an alternative direct method of getting integer input?
• 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
When are the following exceptions thrown:
InputMismatchException
NumberFormatException.
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