错误是因为%d
用于数字非浮点值(int
, long
, etc).
在打印标题的行中,您必须使用%XXs
(其中 XX 是数字)因为您通过了String
s 作为参数:
System.out.format("%10s%15s%15s%15s%20s",
"Grade", "Last Name", "First Name", "Student Number", "Parent Email");
在里面的行中while-loop
,你需要设置%d
为了int
and long
变量,例如年级和学号,无需将其转换为String
using "" + intProperty
:
System.out.format ("%10d%15s%15s%15d%20s",
read.getClass(), read.getLastName(), read.getFirstName(),
read.getStudentNum(), read.getParentEmail());
由于您似乎想要将输出格式化为左侧(而不是右侧),因此您应该在 XX 数字之前添加一个连字符 (-):
//similar for title
System.out.format ("%-10d%-15s%-15s%-15d%-20s",
read.getClass(), read.getLastName(), read.getFirstName(),
read.getStudentNum(), read.getParentEmail());
注意:我假设read.getClass()
and read.getStudentNum()
将返回Grade
and Student number
值作为int
or long
.