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)
So first off you have a lot going here that needs fixing including beyond what I am about to fix for you. If you want to output all the classes at the end together you will have to edit my code to store the formatted Strings in a array then output them all together.
I fixed your error by adding
scanner.nextLine();after yourint numberofcourses = scanner.nextInt();line. You need to do this becausescanner.nextInt()does not consume the line terminator and move to the next line, so this means your nextnextLine()will mess up and attempt to read nothing. This extra line ensures thatScannerwill move to the next line.Moving on, you have a whole bunch of improperly used
scanner.hasNextLine();. This returns a boolean condition that lets you know if the next line exists. You currently do not use this boolean condition and just have it doing nothing. If you want to use this you need to use it in either anifstatement or awhileloop. I removed all of these lines.Finally to format your print statement use
String.format(). An example of what I did to output your values is shown below. You can store this into aStringarray and output them all together later. Let me know if you any other questions. Here is the complete code(I tested it and it ran fine):And here is what my console looked like:
Obviously the outputs can be formatted in any way you like accordingly, this is just a quick mock up.