You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java输出数据库内容为表格时触发UnknownFormatConversionException异常如何解决

问题根因

你代码中表头的格式化字符串末尾多写了一个单独的%。Java的String.format方法里%是格式占位符的起始标识,单独出现的%无法被识别为合法的格式转换规则,因此抛出UnknownFormatConversionException异常。

修复方案

删除表头格式化字符串末尾多余的%即可,修正后的代码如下:

import java.util.ArrayList;

public class EmployeeManagement
{
    static void display(ArrayList<Employee> al) 
    {
        System.out.println("\n   --------------------Employee List-------------------\n");
        // 已删除末尾多余的%
        System.out.println(String.format("%-10s%-15s%-10s%-20s%-10s", "ID","Name","salary","contact-no","Email-Id"));
        
        for(Employee e : al) 
        {
            System.out.println(String.format("%-5s%-20s%-10s%-15s%-10s", e.id,e.name,e.salary,e.contact_no,e.email_id));
        }
    }
}

补充说明

如果后续需要在格式化输出内容中显示百分号本身,需要写两个连续的%%进行转义,避免触发格式占位符识别逻辑。

内容的提问来源于stack exchange,提问作者simonwsl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 01:57:02