finalized joptionpane fixed

This commit is contained in:
CT
2026-05-07 18:06:27 -05:00
parent 764b9cd2a0
commit 38f90ec94f
3 changed files with 35 additions and 7 deletions

View File

@@ -116,8 +116,8 @@ public class HockeyPlayer {
public String toString() { public String toString() {
String s = String s =
String.format( String.format(
"Name: %s%nGoals: %d\tAssists: %d\tPoints: %d\tPoints Per Game: %.2f%nGames Won: %d" "Name: %s%nGoals: %d Assists: %d Points: %d Points Per Game: %.2f%nGames Won: %d"
+ "\tGames Lost: %d\tGames Played: %d%n", + " Games Lost: %d Games Played: %d%n",
name, name,
goals, goals,
assists, assists,

View File

@@ -161,8 +161,18 @@ public class HockeyStats {
boolean dataExit = false; boolean dataExit = false;
int k; int k;
try { try {
k = Integer.parseInt(JOptionPane.showInputDialog(menu)); String input = JOptionPane.showInputDialog(menu);
if (input == null) {
throw new NumberFormatException("Cancel or Exit clicked");
}
if (input == "") {
throw new IllegalArgumentException("No input");
}
k = Integer.parseInt(input);
} catch (Exception e) { } catch (Exception e) {
if (e instanceof NumberFormatException) {
return true;
}
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!"); JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
return false; return false;
} }
@@ -202,6 +212,7 @@ public class HockeyStats {
case 2: case 2:
String rmName = String rmName =
JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:"); JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:");
if (rmName == null) break;
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
players.removePlayer(rmName) players.removePlayer(rmName)
@@ -277,8 +288,18 @@ public class HockeyStats {
boolean statsExit = false; boolean statsExit = false;
int k; int k;
try { try {
k = Integer.parseInt(JOptionPane.showInputDialog(menu)); String input = JOptionPane.showInputDialog(menu);
if (input == null) {
throw new NumberFormatException("Cancel or Exit clicked");
}
if (input == "") {
throw new IllegalArgumentException("No input");
}
k = Integer.parseInt(input);
} catch (Exception e) { } catch (Exception e) {
if (e instanceof NumberFormatException) {
return true;
}
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!"); JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
return false; return false;
} }
@@ -302,8 +323,15 @@ public class HockeyStats {
JOptionPane.showMessageDialog(null, "Sorted by points"); JOptionPane.showMessageDialog(null, "Sorted by points");
break; break;
case 5: case 5:
int minGoals = String minInput = JOptionPane.showInputDialog(null, "Enter the min goals to show:");
Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the min goals to show:")); if (minInput == null) {
break;
}
if (minInput.equals("")) {
JOptionPane.showMessageDialog(null, "Enter a value");
break;
}
int minGoals = Integer.parseInt(minInput);
PlayerList pl = players.getPlayersGoalsAbove(minGoals); PlayerList pl = players.getPlayersGoalsAbove(minGoals);
JOptionPane.showMessageDialog(null, pl.toString()); JOptionPane.showMessageDialog(null, pl.toString());
break; break;

View File

@@ -49,7 +49,7 @@ public class PlayerList {
public String toString() { public String toString() {
String s = ""; String s = "";
for (HockeyPlayer p : pList) { for (HockeyPlayer p : pList) {
s += p.toString(); s += p.toString() + "\n";
} }
return s; return s;
} }