线程 "main" java.util.MissingFormatArgumentException 中的异常:格式说明符“%s”
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'
我正在尝试 运行 我的程序,它在控制台中显示了这个,我试图实现或在控制台中显示在 ATM 中创建了一个新用户,但它只显示用户名、ID,不显示日期。根据控制台,这是似乎是问题的代码块
User.Java
//print log message
System.out.printf("New user %s, %s with ID %s created. \n",
firstName, this.uuid );
Bank.java
public User addUser(String firstName, String lastName, String pin){
User newUser = new User(firstName, lastName, pin, this);
this.users.add(newUser);
//create a savings account fot he user
Account newAccount = new Account("Savings", newUser, this);
newUser.addAccount(newAccount);
this.accounts.add(newAccount);
return newUser;
}
Atm.java
User aUser = theBank.addUser("Carl", "Bustamante", "0605");
这是控制台上显示的内容。
enter image description here
希望得到您的帮助,谢谢。
根本原因
System.out.printf
& String.format
要求提供与说明符相同数量的参数,否则会触发此异常。
- 在你的例子中,你提供了
%s
的 3 个说明符,但只有 2 个参数,firstname
和 this.uuid
:
System.out.printf("New user %s, %s with ID %s created. \n", firstName, this.uuid );
从上下文来看,您似乎是先输入名字和姓氏,然后再输入 UUID?
关于 Easy 的注释 Prevention/Detection
现代 IDE,例如 IntelliJ,通常会突出显示这些类型的容易遗漏的错误。
通常用详细的描述来定义问题并提供解决方案。
例如对于您自己的代码:
我正在尝试 运行 我的程序,它在控制台中显示了这个,我试图实现或在控制台中显示在 ATM 中创建了一个新用户,但它只显示用户名、ID,不显示日期。根据控制台,这是似乎是问题的代码块
User.Java
//print log message
System.out.printf("New user %s, %s with ID %s created. \n",
firstName, this.uuid );
Bank.java
public User addUser(String firstName, String lastName, String pin){
User newUser = new User(firstName, lastName, pin, this);
this.users.add(newUser);
//create a savings account fot he user
Account newAccount = new Account("Savings", newUser, this);
newUser.addAccount(newAccount);
this.accounts.add(newAccount);
return newUser;
}
Atm.java
User aUser = theBank.addUser("Carl", "Bustamante", "0605");
这是控制台上显示的内容。 enter image description here
希望得到您的帮助,谢谢。
根本原因
System.out.printf
&String.format
要求提供与说明符相同数量的参数,否则会触发此异常。- 在你的例子中,你提供了
%s
的 3 个说明符,但只有 2 个参数,firstname
和this.uuid
:System.out.printf("New user %s, %s with ID %s created. \n", firstName, this.uuid );
从上下文来看,您似乎是先输入名字和姓氏,然后再输入 UUID?
关于 Easy 的注释 Prevention/Detection
现代 IDE,例如 IntelliJ,通常会突出显示这些类型的容易遗漏的错误。
通常用详细的描述来定义问题并提供解决方案。
例如对于您自己的代码: