The code here is working fine but the problem lies with the if else statements. Not that they do not work they do. But having said that I would like to clean it up some. When I implemented the if else statement it was to distinguish between Graduate and Undergrad. I would like to consolidate some of this code by removing repeated code. Both of the Graduate and Undergrad are extended classes from a Student class which is abstract.
Any thoughts on this would be appreciated.
<if (choice == 1) { do {//do while block try {//try catch block for exception handeling // collect grades of tests for (int i = 0; i < noTests; i++) { System.out.printf("Enter grade number for %s %s %d: ", grad.getFirstname(), grad.getLastname(), i + 1); grad.setGrades(scan.nextInt()); flag = false; } } catch (InputMismatchException IME) {//catch begin System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput scan.nextLine(); flag = true;//return to top if ture is false } } while (flag);//End do loop do {//do while block try {//try catch block for exception handeling System.out.printf("Enter Midterm grade for: %s %s ", grad.getFirstname(), grad.getLastname()); grad.setmidtermgrad(scan.nextInt()); flag = false; } catch (InputMismatchException IME) {//catch begin System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput scan.nextLine(); flag = true;//return to top if ture is false } } while (flag);//End do loop do { try {//try catch block for exception handeling System.out.printf("Enter final grade for: %s %s ", grad.getFirstname(), grad.getLastname()); grad.setfinalgrade(scan.nextInt()); flag = false; } catch (InputMismatchException IME) {//catch begin System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput scan.nextLine(); flag = true;//return to top if ture is false } } while (flag);//End do loopp //collect information and display results System.out.printf("Average grade for: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getGradeAvg()); System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getFinalGrade()); } else if (choice == 2) {//begin else statement do {//do while block try {//try catch block for exception handeling // collect grades of tests for (int i = 0; i < noTests; i++) { System.out.printf("Enter grade number %d: ", i + 1); Grad.setGrades(scan.nextInt()); } flag = false; } catch (InputMismatchException IME) {//catch begin System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput scan.nextLine(); flag = true;//return to top if ture is false } } while (flag);//End do loop do { try {//try catch block for exception handeling System.out.printf("Enter final grade for: %s %s ", Grad.getFirstname(), Grad.getLastname()); Grad.setfingrade(scan.nextInt()); flag = false; } catch (InputMismatchException IME) {//catch begin System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput scan.nextLine(); flag = true;//return to top if ture is false } } while (flag);//End do loop System.out.printf("Average grade for: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getGradeAvg()); System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getFinGrade()); }>