Skip to content
Advertisement

How to make a grading program using ConsoleIO in Java (***No Scanner***) [closed]

Write a Java application that takes in a number of grade scores (decimals allowed), and computes the average of them. Then print out a letter grade corresponding to the average; either A, B, C, D, or F. See examples below.

Use the following grading scale

At least 90: A otherwise at least 80: B otherwise at least 70: C otherwise at least 60: D otherwise: F

This is what the output should look like. How many grades will you be entering? 3 What is the next grade? 91.5 What is the next grade? 90.5 What is the next grade? 90 Here is the average: 90.66666666666667 This is an A.

here is what I have:

  public class Grades1 {
public static void main(String[] args) {


double total;

double grade;
double scores;

ConsoleIO.printLine("How many grades will you be entering?");
grade = ConsoleIO.readDouble();


scores = ConsoleIO.readDouble();

while (grade < 1) {
    ConsoleIO.printLine("you must enter a grade");
    ConsoleIO.readDouble();
  }

ConsoleIO.printLine("What is the next grade?");
  score = ConsoleIO.readDouble();


 total = ()


    ConsoleIO.printLine("Your grade is ");
    if (total > 90){
        ConsoleIO.printLine("A");
    }
    else if (total > 80) {
        ConsoleIO.printLine("B");
    }
    else if (total > 70) {
                ConsoleIO.printLine("C");
    }
    else if (total > 60) {
        ConsoleIO.printLine("D");
    }
    else {
        ConsoleIO.printLine("F");
    }

} }

Advertisement

Answer

First, make sure your JVM has any console attached to. System.console(); returns the unique Console object associatedwith the current Java virtual machine, if any.

System.console() returns a java.io.Console, which is the proper way to invoke it since Java5 (more or less..)

So you may be hitting a nullPointer if you execute it on a JVM without console.

That said, this is the code:

public static void main(String args[])
{  
    double total=0, score=0;
    int grades=0;
    Console con = System.console(); 
    PrintWriter w = con.writer(); //beware here (NPE)

    while (grades < 1) 
    {
       w.println("How many grades will you be entering? Minimum is 1");
       String numGrades = con.readLine();
       grades = isNumeric(numGrades)?Integer.parseInt(numGrades):-1;
       if (grades<0)
          w.println("Please enter a valid value. You shithead.");
    }
    
    for (int i=0;i<grades;i++) 
    {
       w.println("Enter score for grade nÂș"+(i+1));
       String scoreS = con.readLine();
     
       if (isNumeric(scoreS)) 
          score += Double.parseDouble(scoreS);
       else {
          w.println("Come on man...not again.. Please enter a numeric value..");
          --i;
       }
    }

    total = (score/grades*1D);
    char finalG = getFinalGrade(total);
    w.printf("Your score average is: %f - Grade : %s", total, finalG);
}

public static boolean isNumeric(final String str) 
{
    if (str == null || str.length() == 0) 
        return false;
    for (char c : str.toCharArray()) 
        if (!Character.isDigit(c)) 
            return false;
    return true;
}

public static char getFinalGrade(double total)
{
    if (total >= 90)
       return 'A';
    if (total >= 80) 
       return 'B';
    if (total >= 70) 
       return 'C';
    if (total >= 60) 
       return 'D';
    
    return 'F';
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement