84 Introduction to Java Applications Chapter 2 or
84 Introduction to Java Applications Chapter 2 or as in String firstNumber, secondNumber, result; This is set of names known as a comma-separated list. Once again, notice the comment at the end of each declaration in lines 13 17, indicating the purpose of each variable in the program. Lines 20 21, firstNumber = JOptionPane.showInputDialog( “Enter first integer:” ); use JOptionPane.showInputDialog to allow the user to input the first integer value as a string and store it in firstNumber. Lines 24 25, secondNumber = JOptionPane.showInputDialog( “Enter second integer:” ); use JOptionPane.showInputDialog to allow the user to input the second integer value as a string and store it in secondNumber. Lines 28 29, number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); convert each string input by the user in the input dialogs to type int and assign the values to int variables number1 and number2. Line 32, result = “”; assigns to resultthe empty string a string containing no characters. Every variable declared in a method (such as main) must be initialized (given a value) before it can be used in an expression. Because we do not yet know what the final result string will be, we assign to result the empty string as a temporary initial value. Common Programming Error 2.14 Not initializing a variable defined in a method before that variable is used in the method s body is a syntax error. Lines 34 35, if ( number1 == number2 ) result = result + number1 + ” == ” + number2; define an ifstructure that compares the values of the variables number1 and number2 to determine if they are equal. The ifstructure always begins with keyword if, followed by a condition in parentheses. The if structure expects one statement in its body. The indentation shown here is not required, but it improves the readability of the program by emphasizing that the statement in line 35 is part of the if structure that begins on line 34. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services