72 Introduction to Java Applications Chapter 2 thus, (Web hosting reseller)
72 Introduction to Java Applications Chapter 2 thus, they must appear in all lowercase letters. Chapter 4 summarizes the eight primitive types (boolean, char, byte, short, int, long, floatand double). Line 18 is a single-line comment indicating that the next statement reads the first number from the user. Lines 19 20, firstNumber = JOptionPane.showInputDialog( “Enter first integer” ); reads from the user a String representing the first of the two integers to add. Method JOptionPane.showInputDialogdisplays the input dialog in Fig. 2.10. The argument to showInputDialogindicates what the user should type in the text field. This message is called a prompt, because it directs the user to take a specific action. The user types characters in the text field, and then clicks the OK button or presses the Enter key to return the string to the program. (If you type and nothing appears in the text field, position the mouse pointer in the text field and click the left mouse button to activate the text field.) Unfortunately, Java does not provide a simple form of input that is analogous to displaying output in the command window with System.out s method print and println. For this reason, we normally receive input from a user through a GUI component (an input dialog box in this program). Technically, the user can type anything in the text field of the input. Our program assumes that the user follows directions and enters a valid integer value. In this program, if the user either types a noninteger value or clicks the Cancel button in the input dialog, a runtime logic error will occur. Chapter 14, Exception Handling, discusses how to make your programs more robust by enabling them to handle such errors. This is also known as making your program fault tolerant. The result of JOptionPane method showInputDialog (a String containing the characters typed by the user) is given to variable firstNumberby using the assignment operator, =. The statement (lines 19 20) is read as firstNumber gets the value of JOptionPane.showInputDialog( “Enter first integer” ). The = operator is called a binary operator, because it has two operands: firstNumberand the result of the expression JOptionPane.showInputDialog( “Enter first integer”). This whole statement is called an assignment statement, because it assigns a value to a variable. The expression to the right side of the assignment operator, =, is always evaluated first. In this case, the program calls method showInputDialog, and the value input by the user is assigned to firstNumber. Prompt to the user. When the user clicks OK, showInputDialog returns the 45 typed by the user to the program as a String. The program must convert the Stringto an integer. Text field in which the user types a value. Fig. 2.10 Input dialog box. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01