I'm trying to create a program that produces a grade report for a student. I'd like to ask the following questions please:
1 - When I run the program, I encounter the problem Exception in thread "main" java.util.InputMismatchException (please see below for details).
2 - How do I format the output to be a table? I plan to have 5 columns: Class, Description, Units, Grade, and Grade Points. I cannot get the contents in the table aligned with the headings (Class, Description, Units, etc.)
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 a[] = new String[numberofcourses];
//Arrays for class number, description, units, grade, grades point
//Here, input class number, description, units, and grades
for(int i = 0; i < numberofcourses; i++)
{
System.out.println("Please enter the #"+(i+1)+" class name: ");
String ClassName = scanner.nextLine();
scanner.hasNextLine();
System.out.println("Please enter the #"+(i+1)+" description: ");
String Description = scanner.nextLine();
scanner.hasNextLine();
System.out.println("Please enter the #"+(i+1)+" units: ");
int Units = scanner.nextInt();
scanner.hasNextLine();
int Grades = scanner.nextInt();
scanner.hasNextLine();
}
System.out.println("Class Grades - "+term+" Term");
System.out.println("Office Grades");
System.out.println("Class \t \t Description \t \t Units \t \t Grade \t \t Grade Points");
for(int i = 0; i < numberofcourses; i++)
{
System.out.println(a[i] + "\t \t");
}
}
}
Output:
Please enter the term of your grade calculation (for example, Fall 2015):
Fall 2016
Please enter the number of courses that you are enrolled in Fall 2016:
2
Please enter the #1 class name:
COMPSCI 172
Please enter the #1 description:
Computer Science
Please enter the #1 units:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Project1.main(Project1.java:29)