0

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)
  • Maybe the scanner was not able to parse what you entered into the console? For example, it asked for a number, you entered "hello"? do you really want to save units as an int? or a float? Checkout https://docs.oracle.com/javase/1.5.0/docs/api/java/util/InputMismatchException.html – BugsForBreakfast Jun 20 '19 at 16:56

1 Answers1

0

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 your int numberofcourses = scanner.nextInt(); line. You need to do this because scanner.nextInt() does not consume the line terminator and move to the next line, so this means your next nextLine() will mess up and attempt to read nothing. This extra line ensures that Scanner will 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 an if statement or a while loop. 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 a String array 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):

    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();
    scanner.nextLine();

    //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();

        System.out.println("Please enter the #"+(i+1)+" description: ");
        String Description = scanner.nextLine();

        System.out.println("Please enter the #"+(i+1)+" units: ");
        int Units = scanner.nextInt();

        System.out.println("Please enter the #"+(i+1)+" grades: ");
        int Grades = scanner.nextInt();

        System.out.println("Class Grades - "+term+" Term");
        System.out.println("Office Grades");
        System.out.println(String.format("Class: %s Description: %s Units: %d Grade: %d Grade Points", ClassName, Description, Units, Grades));
    }

And here is what my console looked like:

Please enter the term of your grade calculation (for example, Fall 2015): 
Fall 2012
Please enter the number of courses that you are enrolled in Fall 2012: 
1
Please enter the #1 class name: 
Comp Sci 2001
Please enter the #1 description: 
Computer Science
Please enter the #1 units: 
3
Please enter the #1 grades: 
95
Class Grades - Fall 2012 Term
Office Grades
Class: Comp Sci 2001 Description: Computer Science Units: 3 Grade: 95 Grade Points

Obviously the outputs can be formatted in any way you like accordingly, this is just a quick mock up.

Nexevis
  • 4,647
  • 3
  • 13
  • 22