I'm creating a java program for an assignment that is supposed to take a test score 0-100, that throws an exception if the score entered is outside of that range. Once the error message is displayed showing that the test score entered is not valid there is supposed to be a confirm dialog that asks if they want to enter another test score. However, I can't seem to figure out how to get the confirm dialog to work. I've looked through the documentation on Oracles website, but can't seem to make heads or tales of it. This is what I have so far.
import javax.swing.*; public class UseScoreException { static String input; static private double score; public static void main(String[] args) { input = JOptionPane.showInputDialog(null, "Enter Test Score for Student: "); score = Double.parseDouble(input); try { Score Test = new Score(score); JOptionPane.showMessageDialog(null,"That is a Valid Score"); } catch( ScoreException scorExc) { JOptionPane.showMessageDialog(null, "The Score Must be >= 0 or <= 100!"); } JOptionPane.showConfirmDialog(null, "Do You Want to Enter a New Score?", input, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); } }import javax.swing.JOptionPane; public class ScoreException extends Exception { public ScoreException() { super("Invalid Score"); } }At first I thought the Confirm dialog might go inside the catch, however it seems after thinking about it, that it's more likely to go after the catch considering I have to add another seperate catch to account for invalid characters. When I run the application it doesn't show at all.import javax.swing.JOptionPane; public class Score { static double score; public Score(double testScore) throws ScoreException { score = testScore; if (score < 0 || score > 100) throw (new ScoreException()); } }
--- Update ---
Upon further investigation, I found a way to consider if an open is yes or no, however, running an if statement seems ineffective to have the user enter another test score.
Is there perhaps another way to accomplish this?[COLOR="Silver"]
--- Update ---
Here's what I come up with so far. I can't seem to come up with a way to run the user input for the test score again without posting the entire user input and try/catch methods again. Help >.<
import javax.swing.*; public class UseScoreException { static String input; static private double score; static boolean isYes; static int selection; public static void main(String[] args) { input = JOptionPane.showInputDialog(null, "Enter Test Score for Student: "); score = Double.parseDouble(input); try { Score Test = new Score(score); JOptionPane.showMessageDialog(null,"That is a Valid Score"); } catch( ScoreException scorExc) { JOptionPane.showMessageDialog(null, "The Score Must be >= 0 or <= 100!"); } selection = JOptionPane.showConfirmDialog(null, "Do You Want to Enter Another Score?", "Data Input Error", JOptionPane.YES_NO_OPTION); isYes = (selection == JOptionPane.YES_OPTION); if(isYes == true) } }
--- Update ---
Here's what I come up with so far. I can't seem to come up with a way to run the user input for the test score again without posting the entire user input and try/catch methods again. Help >.<
import javax.swing.*; public class UseScoreException { static String input; static private double score; static boolean isYes; static int selection; public static void main(String[] args) { input = JOptionPane.showInputDialog(null, "Enter Test Score for Student: "); score = Double.parseDouble(input); try { Score Test = new Score(score); JOptionPane.showMessageDialog(null,"That is a Valid Score"); } catch( ScoreException scorExc) { JOptionPane.showMessageDialog(null, "The Score Must be >= 0 or <= 100!"); } selection = JOptionPane.showConfirmDialog(null, "Do You Want to Enter Another Score?", "Data Input Error", JOptionPane.YES_NO_OPTION); isYes = (selection == JOptionPane.YES_OPTION); if(isYes == true) } }