格式化Java输出像表格一样 [英] Formatting Java Output Like a Table

查看:403
本文介绍了格式化Java输出像表格一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以表格式的格式输出关于我的程序存储的学生信息,因为\并不总是提供正确的间隔。为了这样做,我遇到了这个问题,并试图启用类似的解决方案。然而,当我试图执行它的时候,我得到了代码中的格式行错误。



pre $ public void displayStudents ){
System.out.println(\\\
-----------------------------);
System.out.println(电子邮件系统 - 显示学生);
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());
//这将输出每个字段的字符空间的数量,给列表一个类似于表格的质量
}
} //结束显示学生

代码的目标是以类似于以下图像的方式输出。



请帮助我找到我的错误。有没有可能的方法来执行此?



谢谢。

编辑:错误得到的是

 线程main中的GradeException java.util.IllegalFormatConversionException:d!= java.lang.String 
在java.util.Formatter $ FormatSpecifier.failConversion(未知源)
在java.util.Formatter $ FormatSpecifier.printInteger(未知源)
在java.util.Formatter $ FormatSpecifier.print(Unknown Source )
在java.util.Formatter.format(未知源)
在java.io.PrintStream.format(未知源)
在StudentList.displayStudents(StudentList.java:184)$ b (OnlineCommunications.java:403)$ b $在OnlineCommunications.main(OnlineCommunications.java:451)

应该注意的是,Grade是一个整数,Long是一个double。

解决方案

因为%d 是数值非浮点值( int long 等)。

在打印标题的行中,您必须使用%XXs (其中XX是数字),因为您正在传递 String s作为参数:

  System.out.format(%10s%15s%15s%15s%20s ,
年级,姓氏,名字,学生号码,家长电子邮件);

while循环 ,你需要为 int long %d >变量,例如Grade和Student Number,不需要使用+ intProperty :转换为 / b>

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

由于它看起来像要将输出格式化为左侧(而不是右侧)你应该在XX号码之前加一个( - )符号:

  //类似于title 
System.out .format(%-10d%-15s%-15s%-15d%-20s,
read.getClass(),read.getLastName(),read.getFirstName(),
read.getStudentNum (),read.getParentEmail());

注意:我假设 read.getClass() read.getStudentNum()会返回成绩学号的值为 int long


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.

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.

解决方案

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.

这篇关于格式化Java输出像表格一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆