Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Running with Exceptions

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Running with Exceptions

    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");
    	}
     
    }
    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());
    		}
    }
    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.

    --- 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)
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Running with Exceptions

    If you want to give the user an option to enter another score then you are going to need a loop of some kind.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Running with Exceptions

    I figured as much. An if statement for if the boolean value is true, would look into asking for user input again.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Running with Exceptions

    That is a statement. If you are asking can you use an if statement then yes. Pseudocode:
    ask user if they want to enter again
    get response
    if response is no {
        exit loop
    }
    Improving the world one idiot at a time!

Similar Threads

  1. UserDefined Exceptions
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: May 8th, 2012, 05:39 AM
  2. Declaring your own exceptions
    By weakprogrammer in forum Exceptions
    Replies: 4
    Last Post: June 30th, 2011, 08:19 AM
  3. Methods and Exceptions
    By Mandraix in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2011, 07:15 PM
  4. How To add my own Exceptions
    By Newtojava in forum Object Oriented Programming
    Replies: 1
    Last Post: September 2nd, 2010, 07:43 AM
  5. Java Exceptions
    By Vinceisg0d in forum Java Theory & Questions
    Replies: 2
    Last Post: March 13th, 2010, 12:25 AM