Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi! I'm very confused on how to implement try and catch methods, and I'd appreciate any help at all. I've written a program that asks

Hi! I'm very confused on how to implement try and catch methods, and I'd appreciate any help at all. I've written a program that asks for a student's name, how many assignments they've turned in, the grades for each assignment, and then it displays their average and their letter grade. The thing I'm confused about though is how you implement exception handling for the name, number of assignments, and the grades. The program should allow for only strings to be entered into the name section and only integers should be allowed into the number of assignments and the grades. If the user inputs something else, it should throw an exception and allow the user to re-enter their input. How would I go about doing this? I'll attach the code below.

import java.awt.Component; import javax.swing.JOptionPane; //import java.util.*; public class Grades {  private static Component frame;  public static void main(String[] args) {   String studentpro = "";   String studentName = "";   //Scanner input = new Scanner(System.in);   studentName = JOptionPane.showInputDialog("What is the student's name?");   studentpro = JOptionPane     .showInputDialog("How many homework assignments has " + studentName + " submitted this semester?");   int studentprojects = Integer.parseInt(studentpro);   double[] grades = new double[studentprojects];   //double temp = 0;   double total = 0;   {    JOptionPane.showMessageDialog(frame, "Please enter " + studentName + "'s grades, one for each window.");    for (int a = 0; a < grades.length; a++) {     grades[a] = Double.parseDouble(JOptionPane.showInputDialog("Enter Grade " + (a + 1) + "."));    }    for (int i = 0; i < grades.length; i++) {     total = total + grades[i];    }    double average = total / grades.length;    if (average >= 0 && average <= 59.99) {     JOptionPane.showMessageDialog(null, String.format(studentName + "'s average is: %.3f. " + studentName + "'s grade is an F.", average));    }    else if (average >= 60 && average <= 69.99) {     JOptionPane.showMessageDialog(null, String.format(studentName + "'s average is: %.3f. " + studentName + "'s grade is a D.", average));    }    else if (average >= 70 && average <= 79.99) {     JOptionPane.showMessageDialog(null, String.format(studentName + "'s average is: %.3f. " + studentName + "'s grade is a C.", average));    }    else if (average >= 80 && average <= 89.99) {     JOptionPane.showMessageDialog(null, String.format(studentName + "'s average is: %.3f. " + studentName + "'s grade is a B.", average));    }    else if (average >= 90 && average <= 100) {     JOptionPane.showMessageDialog(null, String.format(studentName + "'s average is: %.3f. " + studentName + "'s grade is an A.", average));    }   }  } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions