The following code should add integers from two JTextFields and display the result in resultJTextField. Find the error(s) in the following code:
```
1 try
2 {
3 int first = Integer.parseInt( firstJTextField.getText() );
4 int second = Integer.parseInt( secondJTextField.getText() );
5 int result = first + second;
6 }
7
8 resultJTextField.setText( String.valueOf( result ) );
9
10 catch()
11 {
12 JOptionPane.showMessageDialog( this,
13 "Please enter valid integers", "Number Format Error",
14 OptionPane.ERROR_MESSAGE );
15 }
```
This code segment has two errors. First, there is a line of code between the try block and the catch block. This line must be moved inside the try block. Second, the catch block is not passed any exceptions to catch. The catch block should have one parameter of type NumberFormatException because that is the exception that is likely to occur in the try block.
```
1 try
2 {
3 int first = Integer.parseInt( firstJTextField.getText() );
4 int second = Integer.parseInt( secondJTextField.getText() );
5 int result = first + second;
6 resultJTextField.setText( String.valueOf( result ) );
7 }
8 catch( NumberFormatException exception)
9 {
10 JOptionPane.showMessageDialog( this,
11 "Please enter valid integers", "Number Format Error",
12 OptionPane.ERROR_MESSAGE );
13 }
```
You might also like to view...
Which of the following is a bad case for randomized quickselect?
a. any input with K = 1 b. reverse ordered input c. sorted input d. there are no bad inputs e. none of the above
If a table is created and it is later determined that additional fields should be added to the table, it is usually preferable to
a) delete the existing table and re-create it with the additional fields b) create a parallel table that holds just the additional fields c) re-define an existing field in the table to hold the additional data d) delete the entire database and start over e) alter the table to add the additional fields
Describe the Lightweight Directory Access Protocol.
What will be an ideal response?
What is the advantage of creating a form with a subform?
What will be an ideal response?