Skip to content

Commit

Permalink
Fix: input validity check in converter apps
Browse files Browse the repository at this point in the history
  • Loading branch information
svetter committed Sep 30, 2023
1 parent 24c253d commit 1492855
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/BitrateConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void keyReleased(KeyEvent evt) {
container.add(hSeparator);

resultRateTextfield.setBounds(EDGE, LINE2_Y, FIELD_WIDTH, LINE_HEIGHT);
resultRateTextfield.setText("0");
resultRateTextfield.setText("");
resultRateTextfield.setHorizontalAlignment(SwingConstants.RIGHT);
resultRateTextfield.setEditable(false);
resultRateTextfield.setFont(new Font("Dialog", Font.PLAIN, 18));
Expand Down Expand Up @@ -222,8 +222,14 @@ private String getSelectedRadioButton(ButtonGroup bg) {


private void calculate() {
double check = Double.parseDouble(inputRateTextfield.getText());
if (!(check > 0.)) {
double input;
try {
input = Double.parseDouble(inputRateTextfield.getText());
} catch (java.lang.NumberFormatException e) {
resultRateTextfield.setText("");
return;
}
if (!(input > 0.)) {
resultRateTextfield.setText("0");
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/FilesizeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void keyReleased(KeyEvent evt) {
container.add(hSeparator);

resultSizeTextfield.setBounds(EDGE, LINE2_Y, FIELD_WIDTH, LINE_HEIGHT);
resultSizeTextfield.setText("0");
resultSizeTextfield.setText("");
resultSizeTextfield.setHorizontalAlignment(SwingConstants.RIGHT);
resultSizeTextfield.setEditable(false);
resultSizeTextfield.setFont(new Font("Dialog", Font.PLAIN, 18));
Expand Down Expand Up @@ -179,14 +179,14 @@ private String getSelectedRadioButton(ButtonGroup bg) {


private void calculate() {
double check;
double input;
try {
check = Double.parseDouble(inputSizeTextfield.getText());
input = Double.parseDouble(inputSizeTextfield.getText());
} catch (java.lang.NumberFormatException e) {
resultSizeTextfield.setText("0");
resultSizeTextfield.setText("");
return;
}
if (!(check > 0.)) {
if (!(input > 0.)) {
resultSizeTextfield.setText("0");
return;
}
Expand Down

0 comments on commit 1492855

Please sign in to comment.