diff --git a/HockeyPlayer.java b/HockeyPlayer.java index dab7c94..b7c20f2 100644 --- a/HockeyPlayer.java +++ b/HockeyPlayer.java @@ -116,8 +116,8 @@ public class HockeyPlayer { public String toString() { String s = String.format( - "Name: %s%nGoals: %d\tAssists: %d\tPoints: %d\tPoints Per Game: %.2f%nGames Won: %d" - + "\tGames Lost: %d\tGames Played: %d%n", + "Name: %s%nGoals: %d Assists: %d Points: %d Points Per Game: %.2f%nGames Won: %d" + + " Games Lost: %d Games Played: %d%n", name, goals, assists, diff --git a/HockeyStats.java b/HockeyStats.java index 86cadf5..080a224 100644 --- a/HockeyStats.java +++ b/HockeyStats.java @@ -161,8 +161,18 @@ public class HockeyStats { boolean dataExit = false; int k; 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) { + if (e instanceof NumberFormatException) { + return true; + } JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!"); return false; } @@ -202,6 +212,7 @@ public class HockeyStats { case 2: String rmName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:"); + if (rmName == null) break; JOptionPane.showMessageDialog( null, players.removePlayer(rmName) @@ -277,8 +288,18 @@ public class HockeyStats { boolean statsExit = false; int k; 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) { + if (e instanceof NumberFormatException) { + return true; + } JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!"); return false; } @@ -302,8 +323,15 @@ public class HockeyStats { JOptionPane.showMessageDialog(null, "Sorted by points"); break; case 5: - int minGoals = - Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the min goals to show:")); + String minInput = 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); JOptionPane.showMessageDialog(null, pl.toString()); break; diff --git a/PlayerList.java b/PlayerList.java index 6750bdc..0876569 100644 --- a/PlayerList.java +++ b/PlayerList.java @@ -49,7 +49,7 @@ public class PlayerList { public String toString() { String s = ""; for (HockeyPlayer p : pList) { - s += p.toString(); + s += p.toString() + "\n"; } return s; }