"主线程中的异常:java.util.MissingFormatArgumentException: 格式说明符'10s'缺失"

13

我肯定是在这里漏掉了什么非常明显的东西,但我无法想出来。任何帮助都将不胜感激。错误来自于这里:

package B00166353_Grades;

public class Student{
    String name,banner;

    public Student(String name,String banner){
        this.name=name;
        this.banner=banner;
    }

    public String toString(){
        String productDetails=new String();
        productDetails+=String.format("%-20s%10.2s%10s",this.name,this.banner);
        return productDetails;
    }
}

错误在别的地方。你的主方法在哪里? - Ruchira Gayan Ranaweera
1
不,它就在 toString() 中。只传递了两个参数... - Steve P.
4个回答

30
您的格式字符串"%-20s%10.2s%10s"需要三个参数:
  1. %-20s
  2. %10.2s
  3. %10s
但您只提供了两个参数:
  1. this.name
  2. this.banner
错误消息说明缺少第三个参数(用于%10s)。
因此,您需要调整格式字符串或添加第三个参数。请注意保留原有的HTML标签格式。

4

您有:

productDetails+=String.format("%-20s%10.2s%10s",this.name,this.banner);

由于您的String中有三个%s,因此format()需要三个参数,但是您只传递了this.namethis.banner

另外,由于您在Student内部,您不需要使用this。您可以直接通过namebanner引用它们。


1
你需要给 format 方法添加一个参数,因为你的格式化字符串需要3个参数,而不是两个。

0
productDetails+=String.format("%-20s%10.2s%10s",this.name,this.banner);

我认为你必须传递另一个参数,因为你只传递了名称和横幅,但是在字符串中有3个%..尝试使用仅%-20s%10.2s


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接