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

Redo the grades program with all input in a try block which is inside of a loop

ID: 3647573 • Letter: R

Question

Redo the grades program with all input in a try block which is inside of a loop which continues even if incorrect data is given so that the user has the opportunity to correct the input. The must be at least 2 catch blocks using the actual Java errors for invalid numeric entries or any others that relate to the program you are working with.

This is what I have so far:

import javax.swing.JOptionPane;

public class Class
{
public static void main ( String args[] )
{
String[] name= new String[4];
String[] grade=new String[4];

int i;
int num;
int pass=0;
int fail=0;

for(i=0;i<4;i++)
{
name[i] = JOptionPane.showInputDialog ( "Enter Name:" );
grade[i] = JOptionPane.showInputDialog ( "Enter Grade(1-100): " );
num = Integer.parseInt ( grade[i]);

if(num>60||num==60) pass++;
else fail++;
}

String newline = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder("Name");
sb.append(" "); sb.append("Grade");sb.append(newline);
for(i=0;i<4;i++)
{
sb.append(name[i]);sb.append(" ");sb.append(grade[i]);sb.append(newline);
}

sb.append(newline);
sb.append("Number of PASS=");sb.append(pass);sb.append(newline);
sb.append("Number of FAIL=");sb.append(fail);sb.append(newline);

JOptionPane.showMessageDialog (null,sb.toString());
System.exit ( 0 );
}
}

Explanation / Answer

// hope this helps...and will solve your problem // Code import javax.swing.JOptionPane; import java.io.*; import java.util.Arrays; public class GRADE_BOOK { public static void main(String[] args) { BufferedWriter out; String[] NAMES = new String[200]; String[] ID = new String[200]; Double[] TESTS = new Double[200]; String[] finalGrade = new String[200]; String numStudents, studentName, test, test2, test3, test4, letterGrade; String a = "A", b = "B", c = "C", d = "D", f = "F"; boolean avgTest; int start, numINTStudents; boolean repeat = true; boolean go = false; while (repeat) { // Asks user if they would like to start the gradebook start = JOptionPane.showConfirmDialog (null, "Would you like to activate the grade book"); // If no is selected than the program ends if (start == JOptionPane.NO_OPTION) { JOptionPane.showMessageDialog (null, "THANK YOU, Have a nice day....."); System.exit(0); } // If cancel is selected than the program ends else if (start == JOptionPane.CANCEL_OPTION) { JOptionPane.showMessageDialog (null, "THANK YOU, Have a nice day....."); System.exit(0); } // Asks the user to enter the number of students that they have in the gradebook numStudents = JOptionPane.showInputDialog ("How many students do you have? Enter a number and press OK: "); numStudents = numStudents.replaceAll(" ","").toLowerCase(); try { Integer.parseInt(numStudents); } catch(NumberFormatException e) { go = true; } // while loop that repeats if incorrect input is inserted asks to enter again while (go) { numStudents = JOptionPane.showInputDialog ("In-Valid Input Please try again? Enter a number and press OK: "); numStudents = numStudents.replaceAll(" ","").toLowerCase(); numINTStudents = Integer.parseInt(numStudents); // if incorrect input is inserted then executes try { Integer.parseInt(numStudents); go = false; } catch(NumberFormatException e) { go = true; } } // converts String numStudents into int numINTStudents numINTStudents = Integer.parseInt(numStudents); for (int z = 0; z
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