Java Reference
In-Depth Information
To Enter Applet Code for the try and catch Statements
1. Enter the code in lines 58 through 72 as shown in Figure 4-50.
TextPad displays the try and catch statements in the coding window
(Figure 4-51).
try statement
catch
statement
FIGURE 4-51
If the catch statement executes, a message will display in the applet window,
the TextField will be cleared of any invalid input data, focus will be reset to that
TextField, and any option buttons selected via the user interface will be cleared.
Coding the getSales() Method for the Applet
The getSales() method used in the applet, as shown in Figure 4-52, is similar
to that used in the application. The getSales() method parses the data from the
TextField to a double data type and stores it in a declared variable named sales
(line 77). Then, in line 79, it tests to see if sales is less than or equal to zero. If it
is, the getSales() method throws an exception back to the calling init() method.
If the sales amount is valid, line 81 returns the valid sales amount to the init()
method.
74
75
public double getSales ()
76
{
77
double sales = Double .parseDouble ( salesField.getText ()) ;
78
79
if ( sales <= 0 ) throw new NumberFormatException () ;
80
81
return sales;
82
}
FIGURE 4-52
 
Search WWH ::




Custom Search