我正在尝试以类似表格的格式输出有关我的程序存储的学生的信息,因为 \t 并不总是提供正确的间距。为了做到这一点,我遇到了这个问题并试图启用类似的解决方案。但是,当我尝试执行它时,我的代码中的格式行出现错误。
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
代码的目标是以类似于下图的方式输出。
请帮助我找出我的错误。是否有另一种方法来执行此操作?
谢谢。
编辑:我得到的错误是
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)
需要注意的是,Grade 是一个整数,Long 是一个 double。