2

I am attempting to output information regarding students stored by my program in a tabular-like format as \t does not always provide the correct spacing. In order to do so, I came across this question and have attempted to enable a similar solution. However, I am obtaining errors with the format lines in my code when I attempt to execute it as such.

public void displayStudents (){
    System.out.println ("\n-----------------------------");
    System.out.println ("Email System - Display Students");
    System.out.println ("-----------------------------");
    System.out.format("%10s%15d%15s%15s%20s", "Grade", "Last Name", "First Name", "Student Number", "Parent Email");

    StudentNode current = top;
    while (current != null){
        Student read = current.getStudentNode();
        System.out.format ("%10s%15d%15s%15s%20s", ""+read.getClass(), read.getLastName(), read.getFirstName(), ""+read.getStudentNum(), read.getParentEmail());
        //This will output with a set number of character spaces per field, giving the list a table-like quality
    }
}//End of displayStudents

The goal of the code is to output in a manner similar to the following image. enter image description here

Please aid me in finding my error. Is there perhaps an alternatively method to perform this?

Thanks.

EDIT: The error(s) I am getting are

GradeException in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at StudentList.displayStudents(StudentList.java:184)
at OnlineCommunications.emailOption(OnlineCommunications.java:403)
at OnlineCommunications.main(OnlineCommunications.java:451)

It should be noted that Grade is an integer and Long is a double.

Community
  • 1
  • 1
osama
  • 622
  • 2
  • 10
  • 19

2 Answers2

5

The error is because%d is for numeric non-floating point values (int, long, etc).

In the line where you print the titles, you have to use %XXs (where XX is a number) since you're passing Strings as parameters:

System.out.format("%10s%15s%15s%15s%20s",
    "Grade", "Last Name", "First Name", "Student Number", "Parent Email");

In the line inside the while-loop, you need to set %d for the int and long variables, like Grade and Student Number, there's no need to convert it to String using "" + intProperty:

System.out.format ("%10d%15s%15s%15d%20s",
    read.getClass(), read.getLastName(), read.getFirstName(),
    read.getStudentNum(), read.getParentEmail());

Since it looks like you want to format the output to the left (and not to the right), you should add a hypen (-) symbol before the XX number:

//similar for title
System.out.format ("%-10d%-15s%-15s%-15d%-20s",
    read.getClass(), read.getLastName(), read.getFirstName(),
    read.getStudentNum(), read.getParentEmail());

Note: I assumed read.getClass() and read.getStudentNum() would return the Grade and Student number values as int or long.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • May I know what should you use is the data type is double> – QWERTY Oct 21 '16 at 17:29
  • @2dollar20cents `%f` is what you're looking for. You can read more from the javadoc of [`Formatter`](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html) class. – Luiggi Mendoza Oct 21 '16 at 17:37
4

The problem is:

10s %15d %15s%15s%20s

should be:

10s %15s %15s%15s%20s

This is because all of the input parameters are String, so d (which applies to integral types only) is invalid.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138