Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I hope someone can show me how to implement an error message if it\'s not a doub

ID: 3702821 • Letter: I

Question

I hope someone can show me how to implement an error message if it's not a double written in lengthField, not an integer in iField and if the file doesn't exist in the folder for cField. I would like it to show a warning "Invalid input" and allow new input.

28 public static void main(StringL] args) throws FileNotFoundException 29 30 31 32 JTextField xField new JTextField(5); Restriction(xField, 1); JTextField yField-new JTextField(5); Restriction(yField, 1); JTextField ïengthField = new JTextField(5); RestrictionClengthField, 1); JTextField colorFieldnew JTextField(5); JTextField gridField new JTextField(5); Restriction(gridField, 2); JTextField iterationField new JTextField(5); Restriction(iterationField, 2); // TextField for Re allows only integers or doubles // TextField for Im allows only integers or doubles // TextField for length allows only integers or doubles // TextField for colormap // TextField for grid -allows only integers // TextField for iterations - allows only integers 35 36 37 38 39 41 JPanel Initial-new JPanelO Initial.setLayout(new BoxLayout(Initial, BoxLayout. Y_AXIS)); Initial.add(new JLabelC"Re:" Initial.add(xField); Initial.add(Box.createVerticalStrut(15)); // a spacer Initial.add(new JLabelC"Im:")); Initial.add(yField); Initial.add(Box.createVerticalStrut(15)); // a spacer Initial.add(new JLabelC"Length:")); Initial.add(lengthField); Initial.add(Box.createVerticalStrut(15)); // a spacer Initial.add(new JLabelC"Color:"); Initial.add(colorField) Initial.add(Box.createVerticalStrut(15)); // a spacer Initial.add(new JLabelC"Iterations:"); Initial.add(iterationField); Initial.add(Box.createVerticalStrut(15)); // a spacer Initial.add(new JLabelC" Grid size:")); Initial.add(gridField); 45 46 47 49 51 52 53 56 57 58 59 62 63 64 int result JOptionPane.showConfirmDialog(null, Initial, "Please Enter initial values", JOptionPane .0K.CANCEL-OPTION); if (result-JOptionPane .0K-OPTION) { String xString xField.getTextO; if (xString.equals("")) £ 67 68 69 7% Re-0.10684; } else I Re - Double.parseDouble(xString);

Explanation / Answer

Apply for Every textField that taking input Double and same for other data Type also

step1

if user did not put valid date then its create exception (i.e invalid input.)and execution goes catch block .

step 2: in catch block we handling it.we showing message to user ,reseting textField and allowing user to again put the data.

if again user failed its goes step 1

try {

double1 = Double.parseDouble(JTextField.getText());   //This was a string coming from a textField ..

                     JTextField.requestFocusInWindow();

                     } catch (Exception z) {

                         JOptionPane.showMessageDialog(this, "Incorrect Data Type! Double Numbers Only!",

                            "Inane error", JOptionPane.ERROR_MESSAGE);

                         JTextField.setText("");

                         JTextField.requestFocusInWindow();

                         return;

                }