Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question? Create a program that will input a number grade from a user in a

ID: 639608 • Letter: J

Question

Java Question?

Create a program that will input a number grade from a user in a dialog box. Input it as a string, try to convert it to an integer, and try to see if the number is valid (between 0 & 100, otherwise you would consider it an Exception). Give separate error messages for each type of possible exception, plus include the message and stack trace. For the invalid input (not between 0 and 100), you will have to use an if test and throw the exception yourself (since java wouldn't consider that an exception).

Explanation / Answer

=====================================

Program name GradeChecker.java (Included Stack trace also)

=====================================

import javax.swing.JOptionPane;

public class GradeChecker {

   public static void main(String[] args) {
      
       String grade = JOptionPane.showInputDialog(null,
               "Please enter your grade ", "Grade", 1);

       while (true) {

           try {

               int numberGrade = Integer.parseInt(grade);
               if (numberGrade >= 0 && numberGrade <= 100) {
                   JOptionPane.showMessageDialog(null,
                           "You entered grade is : " + grade, "Grade", 1);
                   break;
               } else {
                   throw new Exception(
                           "Grade must be in between 0 and 100 only");

               }

           } catch (Exception e) {
          
               StackTraceElement stackTraceElement[] = e.getStackTrace();
               String trace ="";
               for(StackTraceElement traceElement: stackTraceElement)
                   trace = trace + traceElement.toString() +" ";
                  
               grade = JOptionPane.showInputDialog(null, "Exception Message: "+e.getMessage().toString()+
                       " Stack Trace: "+trace+
                       " Please re-enter your grade ", "Grade", 1);
           }

       }

   }

}

===================================

Output Screenshots

===================================

https://www.dropbox.com/s/sanshodnfqlptkg/g1.jpg?dl=0

https://www.dropbox.com/s/ll6h2ae1ee0lq2s/g2.jpg?dl=0

https://www.dropbox.com/s/ux44kawqkptwywi/g3.jpg?dl=0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote