From d57cb924cacca62f2898188c7f851d55301e40a9 Mon Sep 17 00:00:00 2001 From: Amy Wilder Date: Wed, 11 Feb 2026 15:29:27 -0500 Subject: [PATCH] Catch NumberFormatExceptions in interactive sim Related to #6 --- src/jls/sim/InterractiveSimulator.java | 88 +++++++++++++++++++++----- 1 file changed, 72 insertions(+), 16 deletions(-) diff --git a/src/jls/sim/InterractiveSimulator.java b/src/jls/sim/InterractiveSimulator.java index c774aff..d02888d 100755 --- a/src/jls/sim/InterractiveSimulator.java +++ b/src/jls/sim/InterractiveSimulator.java @@ -181,8 +181,15 @@ public InterractiveSimulator() { public void actionPerformed(ActionEvent event) { if (scaleField.getText().length() == 0) scaleFactor = 1; - else - scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + else { + try { + scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Scale factor must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } scaleField.setText(scaleFactor+""); traces.setScaleFactor(); if (now != 0) @@ -271,8 +278,15 @@ public void actionPerformed(ActionEvent event) { action.validate(); if (scaleField.getText().length() == 0) scaleFactor = 1; - else - scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + else { + try { + scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Scale factor must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } scaleField.setText(scaleFactor+""); traces.setScaleFactor(); setMaxTime(); @@ -295,14 +309,28 @@ public void actionPerformed(ActionEvent event) { action.validate(); if (scaleField.getText().length() == 0) scaleFactor = 1; - else - scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + else { + try { + scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Scale factor must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } scaleField.setText(scaleFactor+""); traces.setScaleFactor(); if (stepField.getText().length() == 0) stepAmount = 1; - else - stepAmount = Math.max(1,Integer.parseInt(stepField.getText())); + else { + try { + stepAmount = Math.max(1,Integer.parseInt(stepField.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Step amount must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } stepField.setText(stepAmount+""); setMaxTime(); if (sim == null) { @@ -337,14 +365,28 @@ public void actionPerformed(ActionEvent event) { // set up step info if (scaleField.getText().length() == 0) scaleFactor = 1; - else - scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + else { + try { + scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Scale factor must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } scaleField.setText(scaleFactor+""); traces.setScaleFactor(); if (stepField.getText().length() == 0) stepAmount = 1; - else - stepAmount = Math.max(1,Integer.parseInt(stepField.getText())); + else { + try { + stepAmount = Math.max(1,Integer.parseInt(stepField.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Step amount must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } stepField.setText(stepAmount+""); setMaxTime(); @@ -427,8 +469,15 @@ public void actionPerformed(ActionEvent event) { action.validate(); if (scaleField.getText().length() == 0) scaleFactor = 1; - else - scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + else { + try { + scaleFactor = Math.max(1,Integer.parseInt(scaleField.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Scale factor must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } scaleField.setText(scaleFactor+""); traces.setScaleFactor(); paused = false; @@ -476,8 +525,15 @@ public void setMaxTime() { if (tlimit.getText().length() == 0) maxTime = 1; - else - maxTime = Math.max(1,Integer.parseInt(tlimit.getText())); + else { + try { + maxTime = Math.max(1,Integer.parseInt(tlimit.getText())); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, + "Time limit must be an integer", "Error", + JOptionPane.ERROR_MESSAGE); + } + } tlimit.setText(maxTime+""); } // end of setMaxTime method