I'm trying to make a program to returns an equivalent grade to the user's input (A, A-, B+, etc.) and then produces a grade report for the user.
However, I have encounter 2 problems:
My program does not return any grade, except for "Invalid Grade". It does not matter whether I put in A, B, C, B+, or anything.
How do I make a proper 2-D array table with the first row as headers? I plan to have 5 columns: Class, Description, Units, Grade, and Gradepoint. Then after the first row, I'd have the users input in it. With my current code, the program returns something very...funky. :(
I pasted my code below.
Thanks for your help guys!
import java.util.*;
public class Project1 {
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
//Input the term
System.out.println("Please enter the term of your grade calculation (for example, Fall 2015): ");
String term = scanner.nextLine();
//Input the number of courses that the student is enrolled in
System.out.println("Please enter the number of courses that you are enrolled in "+term+": ");
int numberofcourses = scanner.nextInt();
//Declaration
String ClassName[] = new String[numberofcourses];
String Description[] = new String[numberofcourses];
String grade[] = new String[numberofcourses];
int Units[] = new int[numberofcourses];
double gradeValue = 0;
//Arrays for class number, description, units, grade, grade point
//Here, input class number, description, units, and grade
for(int i = 0; i < numberofcourses; i++)
{
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class name: ");
ClassName[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class description: ");
Description[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class units: ");
Units [i] = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class grade: ");
grade[i] = scanner.nextLine();
Map<String, Double> gradeToScore = new HashMap<>();
gradeToScore.put("A", 4.00);
gradeToScore.put("A-", 3.67);
gradeToScore.put("B+", 3.33);
gradeToScore.put("B", 3.00);
gradeToScore.put("B-", 2.67);
gradeToScore.put("C+", 2.33);
gradeToScore.put("C", 2.00);
gradeToScore.put("D+", 1.33);
gradeToScore.put("D", 1.00);
gradeToScore.put("F", 0.0);
gradeToScore.put("FX", 0.0);
if(gradeToScore.containsKey(grade)) {
gradeValue = gradeToScore.get(grade);
}else{
System.out.println("Invalid Grade");
}
}
//Print out the report
//Print out the heading
System.out.println("Class Grades - "+term+" Term");
System.out.println("Office Grades");
//Print out the table
int columns = 5;
int rows = numberofcourses;
String[][] table = new String[rows][columns];
String chain = "";
{
for (int i = 0; i < table.length; i++)
{
for (int c = 0; c < table[0].length; c++)
{
chain += "|" + "Class" + "|" + "Description" + "|" + "Units" + "|" + "Grade" + "|" + "Gradepoints";
}
chain += "|\n";
chain += "|" + ClassName[i] + Description[i] + Units[i] + grade[i] + gradeValue;
}
System.out.println(chain);
}
}
}
Here is my result for the above code:
Please enter the term of your grade calculation (for example, Fall 2015):
Fall 2019
Please enter the number of courses that you are enrolled in Fall 2019:
1
Please enter your #1 class name:
FIN 301
Please enter your #1 class description:
Personal Finance
Please enter your #1 class units:
3
Please enter your #1 class grade:
A
Invalid Grade
Class Grades - Fall 2019 Term
Office Grades
|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|Class|Description|Units|Grade|Gradepoints|
|FIN 301Personal Finance3A0.0
NOTE:
I did try with System.out.format, but the result was not better. Here's my code:
System.out.format("%30s %25s %10s %25s %10s %25 %10 %25 %10 %25", "Class", "|", "Description($)", "|", "Units", "|", "Grade", "|", "Grade Points");
Trial 2 for System.out.format...not any better for sure:
System.out.format("%n%n%n%n%n", "|" + "Class" + "|" + "Description" + "|" + "Units" + "|" + "Grade" + "|" + "Gradepoints");
System.out.format("%n%n%n%n%n", ClassName[i], Description[i], Units[i], grade[i], gradepoint);
There must be some kind of syntax but I must read somewhere for left, right, center alignment, or how much space I wanted to pad. Please know that I'm down to switch to System.out.format as well, if I know the syntax.
Okay, I figured a workaround on my first problem... Here is my partial code for the grade conversion chunk:
I'm still unsure how to do a proper 2-D array table though. :(