1

I'm trying to make a table out for a grade report, which should 5 columns, which are Class, Description, Units, Grade, and Grade Points. However, I cannot make the output aligned with each other. I'm just simply using "\t". Is there a way to make the outputs as a table in Java?

import java.util.*;
public class Project1_Trial2 {
    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];

        //Arrays for class number, description, units, grade, grades point
        //Here, input class number, description, units, and grades
        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();
            System.out.println("Please enter your #"+(i+1)+" class grade: ");
            Grade[i] = scanner.nextLine();
        }

        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(ClassName[i]+"\t \t"+Description[i]+"\t \t"+Units[i]+"\t \t"+Grade[i]);
        }
    }
}
  • For [example](https://stackoverflow.com/questions/15193812/how-to-print-a-table-of-arrays/15194265#15194265) and [example](https://stackoverflow.com/questions/22599302/properly-aligning-strings-on-the-console/22599518#22599518) – MadProgrammer Jun 20 '19 at 21:57

1 Answers1

1

There are various ways to align your output so they align evenly....one way is to replace where you put:

System.out.println(ClassName[i]+"\t \t"+Description[i]+"\t \t"+Units[i]+"\t \t"+Grade[i]);

with:

System.out.printf("%-15s %-15s %-15s ",ClassName[i],Description[i],String.format("%5d", Units[i]),Grade[i]);

instead of using System.out.println() use System.out.printf() that way you can pad the strings and use String.format() inside the printf() function to pad the integer with spaces...you can adjust my padding to whatever you feel fit this is a more efficient way rather than constantly using tabs

KevO
  • 68
  • 1
  • 9