Question: Create another exception class called DuplicateValueException that will be thrown if the user inputs a value that already resides in the array. Modify your solution
Create another exception class called DuplicateValueException that will be thrown if the user inputs a value that already resides in the array. Modify your solution of the previous project to use this new exception class to indicate when a duplicate value is input, in which case an appropriate error message should be displayed. The program should continue normal execution after handling the exception.
// ArrayAccess.java
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ArrayAccess extends JFrame
{
private JTextField inputField;
private JTextField retrieveField1;
private JTextField retrieveField2;
private JTextField outputField;
private JPanel inputArea;
private JPanel retrieveArea;
private JPanel outputArea;
private int num;
private int index = 0;
private int array[] = new int[ 6 ];
private String result;
// set up GUI
public ArrayAccess()
{
super( "Accessing Array values" );
setLayout( new FlowLayout() );
// set up input Panel
inputArea = new JPanel();
inputArea.add( new JLabel( "Enter array elements here" ) );
inputField = new JTextField( 10 );
inputArea.add( inputField );
inputField.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
try {
// reads the number entered in the inputField
int element = Integer.parseInt(inputField.getText());
// Assign element to the next index in the array
array[index] = element;
// Increments instance variable index
index++;
} catch (NumberFormatException ex) {
// Throw NumberFormatException
throw new NumberFormatException("Please enter elements in correct format");
} catch (ArrayIndexOutOfBoundsException ex) {
// Throw ArrayIndexOutOfBoundsException
throw new ArrayIndexOutOfBoundsException("Please enter elements in correct format");
}
inputField.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
// set up retrieve Panel
retrieveArea = new JPanel( new GridLayout( 2, 2 ) );
retrieveArea.add( new JLabel( "Enter number to retrieve" ) );
retrieveField1 = new JTextField( 10 );
retrieveArea.add( retrieveField1 );
retrieveField1.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
try {
// reads the number entered in the inputField
int element = Integer.parseInt(retrieveField1.getText());
String text = "The index's are :";
// Search the element in array
for(int i =0 ; i<6; i++){
// If element is found
if(array[i]==element){
// Print the index where element is found
outputField.setText(text+i+",");
}
else{
// If number is not found throw exception
throw new NumberNotFoundException("Number Not found in array");
}
}
} catch (NumberFormatException ex) {
// Throw NumberFormatException
throw new NumberFormatException("Please enter elements in correct format");
} catch (NumberNotFoundException ex) {
}
retrieveField1.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
retrieveArea.add( new JLabel( "Enter index to retrieve" ) );
retrieveField2 = new JTextField( 10 );
retrieveArea.add( retrieveField2 );
retrieveField2.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
try {
// reads the number entered in the inputField
int index = Integer.parseInt(retrieveField2.getText());
// Store the value at index in number variable
int value = array[index];
// Output the value found at index
outputField.setText(Integer.toString(value));
} catch (NumberFormatException ex) {
// Throw NumberFormatException
throw new NumberFormatException("Please enter elements in correct format");
}catch (ArrayIndexOutOfBoundsException ex) {
// Throw ArrayIndexOutOfBoundsException if index not found
throw new ArrayIndexOutOfBoundsException("Please enter correct index ");
}
retrieveField2.setText( "" );
} // end anonymous inner class
} // end new ActionListener
); // end call to addActionListener
// set up output Panel
outputArea = new JPanel();
outputArea.add( new JLabel( "Result" ) );
outputField = new JTextField( 30 );
outputField.setEditable( false );
outputArea.add( outputField );
add( inputArea );
add( retrieveArea );
add( outputArea );
} // end constructor
} // end class ArrayAccess
// ArrayAccessTest.java import javax.swing.JFrame; public class ArrayAccessTest { // execute application public static void main( String args[] ) { ArrayAccess application = new ArrayAccess(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); application.setSize( 400, 200 ); application.setVisible( true ); } } // end class ArrayAccessTest
// NumberNotFoundException.java public class NumberNotFoundException extends Exception { // no-argument constructor specifies default error message public NumberNotFoundException() { super( "Number not found in array" ); } // constructor to allow customized error message public NumberNotFoundException( String message ) { super( message ); } } // end class NumberNotFoundException
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
