Sie sind auf Seite 1von 4

CS 1136 LAB 3

Behavior of Scanner input with Various Data Types and Unexpected Input
One of the goals for this lab is to understand how the input buffer works with the Scanner input function. When using the input function by itself, a space will act like pressing the return key. It symbolizes an end of line to the Scanner. The String variable will accept any character the keyboard produces: letters, numbers, or symbols. The int type variables will accept only a leading sign (a blank, + or -) and numbers . Finally, float type variables will accept a leading sign, numbers and a period for the decimal. Input from the keyboard not used by a Scanner call will remain in the buffer. Exercise 1: Enter the following program into the compiler of your choice, save it on your hard drive and compile it. Refer to Labs 1 and 2 if you need assistance. (Note: If you copy the text from this lab into your compiler, the double quote characters need to be replaced in your code with double quotes recognized by the compiler).

import java.util.Scanner; public class Lab3_Ex1 { public static void main (String [] args) { Scanner keyboard = new Scanner(System.in);
float x; int y; char ch1, ch2; String name; String input; System.out.print( "Enter input = keyboard.next(); ch1 = input.charAt(0); System.out.print( "Enter y = keyboard.nextInt(); System.out.print( "Enter input = keyboard.next(); ch2 = input.charAt(0); System.out.print( "Enter a character: "); a number: "); another character: "); a name: ");

name = keyboard.next(); System.out.print( "Enter a floating point value: "); x = keyboard.nextFloat(); System.out.println("\nch1 = " + ch1); System.out.println( "y = " + y); System.out.println( "ch2 = " + ch2); System.out.println( "Name is " + name); System.out.println( "x = " + x); System.exit(0); } }
Run the above program using the following sets of data and answer the questions below. Type your answers into a text file, name the file Lab3_Ex1_output and upload the file to ELearning. 1. Enter x 1234 y john 10.50 (Note: enter all the characters on a single press the enter key line and press the enter key). Why did all the prompts after the first one run together and not stop for you to enter data? Enter 1 89 j 110.50 press the enter key What is the computer waiting for? Enter a 1, then explain why the output is the way it is. Exercise 2: 1. If you were to run the following program and enter John Smith as the name what would the output be?

2.

import java.util.Scanner; public class Lab3_Ex2 { public static void main (String [] args) { Scanner keyboard = new Scanner(System.in);
String name; System.out.print( What is your name? ); name = keyboard.next(); System.out.println( Hello + name); System.exit(0); } }

2. Modify Lab3_Ex2 to accept the complete name when John Smith is entered.

Behavior of System.out with various print methods


Output formatting In order to write messages to the screen, we have been using a statement that looks like:

System.out.println( "Hello"); The println method inserts an end-of-line character and forces the next output to begin on the next line. We can use successive calls to println to create
blank lines. For example,

System.out.println( "Happy"); System.out.print("\nNew Year"); System.out.println( "!");


Output: Happy New Year! Notice that even though the exclamation point is printed using its own System.out.println statement, it is not printed on its own line because println or a \n was not used at the end of the preceding System.out statement. The println statements can be replaced by using \n. The same code as above could be written in the following manner:

System.out.print( Happy\n\nNew Year); System.out.println( !; Exercise 3: JOptionPane dialog boxes allow graphical input and output. The showMessageDialog method displays a String of information while the ShowInputDialog method allows the user to enter a String of information. ShowInputDialog operates like the nextLine method from the Scanner class. In order to use an input value from showMessageDialog, the programmer must convert the String to a number by using one of the parse methods (e.g. Double.parseDouble or Integer.parseInt). A single character from a String can be stored in a character variable by using the .charAt() method from the String class (e.g. If the String variable input receives the input value from JOptionPane.showInputDialog, then char x = input.charAt(0); stores the first character from input into the variable x).

Re-write Lab3_Ex1.java to use JOptionPane dialog boxes for all input and output operations. Name the new source Lab3_Ex3.java. Note that input from Exercise 1, steps 1 and 2, will not work with the JOptionPane input dialogs. Enter the data from Exercise 1, step 1 into your new program such that your Lab3_Ex3 program displays correct output. The data types for each of the variables in Lab3_Ex1.java should not be changed from the original data types. Change only the input and output operations in the program.
Please upload and submit your .java files for the three exercises and the output files for exercises one and two to ELearning.

Das könnte Ihnen auch gefallen