Home > OS >  How to show only the valid grades in output not include the invalid grades using java
How to show only the valid grades in output not include the invalid grades using java

Time:09-29

I made the invalid grades into " " but it seems in Grade Summary Report output it's showing space like this https://i.stack.imgur.com/3kHZW.png how to show only the valid grades? the invalid grades is making blank space in output

This is the question https://i.stack.imgur.com/uxLhG.png

This is the needed output https://i.stack.imgur.com/HNZpZ.png

This is my Program

String [][] student = new String[100][3];
int stu = 0, totalp=0, totalf=0;

for(int h=0; h<100; h  ) {
    System.out.print("Enter Name: ");
    student[h][0] = sc.nextLine();
    System.out.print("Enter Grade: ");
    student[h][1] = sc.nextLine();

    int grade = Integer.parseInt(student[h][1]);
    if(grade >100 || grade <50) {
        student[h][0] = "";
        student[h][1] = "";
        student[h][2] = "";
        System.out.println("Invalid Grade");
    }
    else if(grade >=75) {
        student[h][2] = ("Passed");
        totalp  ;
    }
    else if(grade <=74) {
        student[h][2] = ("Failed");
        totalf  ;
    }
    System.out.print("Add new record (Y/N)?: ");
    char choice = sc.nextLine().charAt(0);
    System.out.println("");
    if(choice == 'y' || choice =='Y') {
        stu  ;
        continue;
    }
    else if(choice == 'n' ||choice =='N') {
        break;
    }
}
System.out.println("\nGRADE SUMMARY REPORT");
System.out.println("\nName\tGrades\tRemarks");
for(int i =0; i<stu 1; i  ) {
    for(int h =0; h<student[i].length; h  ) {
        System.out.print(student[i][h]   "\t");
    }
    System.out.println();
}
System.out.println("\nTotal Passed: "   totalp);
System.out.println("Total Failed: "  totalf);

CodePudding user response:

If a grade entered by the user is invalid, just don't add it to student array. Then it won't get printed at all. You should also handle the case where the user does not enter a number when asked to enter a grade.

When asking the user if she wants to add a new record, you only need to check whether she entered a Y. Anything else can be considered a no.

The below code uses method printf to format the report. Also, in order to format the report, I keep track of the longest name entered by the user.

import java.util.Scanner;

public class Students {
    private static final int MAX_ROWS = 100;
    private static final int NAME = 0;
    private static final int GRADE = 1;
    private static final int REMARK = 2;

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String[][] student = new String[MAX_ROWS][3];
        int totalp = 0, totalf = 0, longest = 0;
        int h = 0;
        while (h < MAX_ROWS) {
            System.out.print("Enter Name: ");
            String name = sc.nextLine();
            int len = name.length();
            if (len > longest) {
                longest = len;
            }
            System.out.print("Enter Grade: ");
            String grade = sc.nextLine();
            try {
                int mark = Integer.parseInt(grade);
                if (mark >= 50  &&  mark <= 100) {
                    student[h][NAME] = name;
                    student[h][GRADE] = grade;
                    if (mark >= 75) {
                        student[h][REMARK] = "Passed";
                        totalp  ;
                    }
                    else {
                        student[h][REMARK] = "Failed";
                        totalf  ;
                    }
                    h  ;
                    if (h == MAX_ROWS) {
                        System.out.println("Maximum students entered.");
                    }
                    else {
                        System.out.print("Add new record (Y/N)?: ");
                        String choice = sc.nextLine();
                        if (!"Y".equalsIgnoreCase(choice)) {
                            break;
                        }
                    }
                }
                else {
                    throw new NumberFormatException();
                }
            }
            catch (NumberFormatException xNumberFormat) {
                System.out.println("Invalid. Grade is a number between 50 and 100.");
            }
            System.out.println();
        }
        System.out.println("\nGRADE SUMMARY REPORT\n");
        if (longest < 4) {
            longest = 4;;
        }
        System.out.printf("%-"   longest   "s", "Name");
        System.out.println("   Grade  Remarks");
        String format = "%-"   longest   "s  %4s    %s%n";
        for (int i = 0; i < h; i  ) {
            System.out.printf(format, student[i][NAME], student[i][GRADE], student[i][REMARK]);
        }
        System.out.println();
        System.out.println("Total passed: "   totalp);
        System.out.println("Total failed: "   totalf);
    }
}

Here is output from a sample run of the above code:

Enter Name: Superman
Enter Grade: 99
Add new record (Y/N)?: y

Enter Name: Batman
Enter Grade: 23
Invalid. Grade is a number between 50 and 100.

Enter Name: Iron Man
Enter Grade: 74
Add new record (Y/N)?: n

GRADE SUMMARY REPORT

Name       Grade  Remarks
Superman    99    Passed
Iron Man    74    Failed

Total passed: 1
Total failed: 1

You may also want to refer to the Exceptions lesson in Oracle's Java tutorials.

CodePudding user response:

right now you are still printing the invalid grades in a line. what you want is to check and make sure the grade is valid before printing it. Since you made all the invalid grade an empty string, you can simply check for that like so

if(!student[i][h].isEmpty()){
  System.out.print(student[i][h]   "\t");
}                   
  • Related