从 String 转换为 int 到 Vaadin 时出错
Error converting from String to int to Vaadin
我正在尝试在 Vaadin 中将 String 转换为 int。这是代码:
TextField name = new TextField();
int num;
num = Integer.parseInt(String.valueOf(name.getValue()));
Paragraph greeting = new Paragraph("");
Button button = new Button("Result", event -> {
greeting.setText(" " + num * 500);
});
add(name, button, greeting);
这里是错误:
There was an exception while trying to navigate to '' with the exception message 'Error creating bean with name 'com.gmail.ilim.MainView': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.gmail.ilim.MainView]: Constructor threw exception; nested exception is java.lang.NumberFormatException: For input string: ""'
正如评论中的其他人所说,一种解决方案是捕获异常:
try {
num = Integer.parseInt(name.getValue());
} catch (NumberFormatException nfe) {
num = 1; // your default value
}
...
正如评论中所说:
1) 仅在按钮的点击监听器中解析输入值,而不是直接在视图的构造函数中解析(此时,TextField
将始终为空值)
2) 捕获 NumberFormatException。即使处理了第 1 点,用户也始终可以输入无法解析为 Integer
的非数字内容
TextField name = new TextField();
Paragraph greeting = new Paragraph("");
Button button = new Button("Result", event -> {
int num;
try {
num = Integer.parseInt(String.valueOf(name.getValue()));
} catch (NumberFormatException e) {
num = 0; // your default value
// you should also let the user know he didnt enter a valid number
Notification.show("Please enter a valid number");
}
greeting.setText(" " + num * 500);
});
add(name, button, greeting);
另一种可能性是直接使用 IntegerField
而不是 TextField
。这仅适用于 Vaadin 14。1.x
我想到的另一种可能性是使用活页夹 - 绑定文本字段时,您可以添加 StringToIntegerConverter
。这会有点复杂,我不会为此而走那条路。
我正在尝试在 Vaadin 中将 String 转换为 int。这是代码:
TextField name = new TextField();
int num;
num = Integer.parseInt(String.valueOf(name.getValue()));
Paragraph greeting = new Paragraph("");
Button button = new Button("Result", event -> {
greeting.setText(" " + num * 500);
});
add(name, button, greeting);
这里是错误:
There was an exception while trying to navigate to '' with the exception message 'Error creating bean with name 'com.gmail.ilim.MainView': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.gmail.ilim.MainView]: Constructor threw exception; nested exception is java.lang.NumberFormatException: For input string: ""'
正如评论中的其他人所说,一种解决方案是捕获异常:
try {
num = Integer.parseInt(name.getValue());
} catch (NumberFormatException nfe) {
num = 1; // your default value
}
...
正如评论中所说:
1) 仅在按钮的点击监听器中解析输入值,而不是直接在视图的构造函数中解析(此时,TextField
将始终为空值)
2) 捕获 NumberFormatException。即使处理了第 1 点,用户也始终可以输入无法解析为 Integer
TextField name = new TextField();
Paragraph greeting = new Paragraph("");
Button button = new Button("Result", event -> {
int num;
try {
num = Integer.parseInt(String.valueOf(name.getValue()));
} catch (NumberFormatException e) {
num = 0; // your default value
// you should also let the user know he didnt enter a valid number
Notification.show("Please enter a valid number");
}
greeting.setText(" " + num * 500);
});
add(name, button, greeting);
另一种可能性是直接使用 IntegerField
而不是 TextField
。这仅适用于 Vaadin 14。1.x
我想到的另一种可能性是使用活页夹 - 绑定文本字段时,您可以添加 StringToIntegerConverter
。这会有点复杂,我不会为此而走那条路。