Checking for invalid data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dave55
    New Member
    • Feb 2010
    • 1

    Checking for invalid data

    This is my code....

    Code:
    import javax.swing.JOptionPane;
    public class AdmissionsSpence
    {
       public static void main(String[] args)
       {
    	String entranceScore, gradePointAverage;
    	gradePointAverage = JOptionPane.showInputDialog(null, "Enter your G.P.A.");
    	entranceScore = JOptionPane.showInputDialog(null, "Enter your Entrance score");
    	FinalResults(entranceScore, gradePointAverage);
    	}
    	public static void FinalResults(String entranceScore,String gradePointAverage)
    	{
    	
    	double gPaverage;
    	int entScore;
    	gPaverage = Double.parseDouble(gradePointAverage);
    	entScore = Integer.parseInt(entranceScore);
    	if (gPaverage >= 3.0 && entScore > 60)
    	JOptionPane.showMessageDialog(null, "Congradulations!");
    	else if (entScore >= 85)
    	JOptionPane.showMessageDialog(null, "Congradulations!");
    	else 
    	JOptionPane.showMessageDialog(null, "Try again next year!");
    	
    	JOptionPane.showMessageDialog(null, "Invalid entry");
    	System.exit(0);
    	}
    }
    Now, I need to write some code to do some error checking of the data. That is, if the grade point average is not between 0.0 and 4.0 or if the entrance score is not between 0 and 100, then I need an appropriate error message to the screen telling the user that they entered invalid data. How would I go about this and where would I place the code, this is driving me nuts. I already have a JOptionPane.sho w.Message Dialog (null, Invalid entry); but nothing else.

    Thank you
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Make an if-statement like the one that you already have inside your code, but with different values (condition).
    Place it right before your first if-statement.
    Place the error message inside this if-statement.

    Comment

    Working...