简体   繁体   English

像表一样格式化Java输出

[英]Formatting Java Output Like a Table

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. 我试图以类似于表格的格式输出关于我的程序存储的学生的信息,因为\\ t并不总是提供正确的间距。 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. 代码的目标是以类似于下图的方式输出。 在此输入图像描述

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. 应该注意,Grade是一个整数,Long是一个整数。

The error is because %d is for numeric non-floating point values ( int , long , etc). 错误是因为%d用于数字非浮点值( intlong等)。

In the line where you print the titles, you have to use %XXs (where XX is a number) since you're passing String s as parameters: 在打印标题的行中,您必须使用%XXs (其中XX是数字),因为您将String作为参数传递:

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 : while-loop内的行中,你需要为intlong变量设置%d ,比如Grade和Student Number,不需要使用"" + intProperty将它转换为String

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: 由于看起来您想要将输出格式化为左侧(而不是右侧),您应该在XX号码之前添加一个超级( - )符号:

//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 . 注意:我假设read.getClass()read.getStudentNum()会将GradeStudent number数值返回为intlong

The problem is: 问题是:

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

should be: 应该:

10s %15s %15s%15s%20s 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. 这是因为所有输入参数都是String ,因此d仅适用于整数类型 )无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM