将 TextField 值转换为 Int
我有一个文本字段,我可以在其中输入值;我需要将键入的值转换为 Int 并使用 SOPrintln 显示它。
EDIT:
我现在已经尝试过:
try {
int i = Integer.parseInt(myText.getText());
System.out.println("This is an integer " + i);
}
catch (NumberFormatException x) {
System.out.println("Error : "+ x.getMessage());
}
但它给了我:
This is an integer 122 // When I type 122
Error : For input string: "122,5" // When I type 122,5
Error : For input string: "122.5" // When I type 122.5
任何想法。
编辑:
就是这样:
double d = Double.parseDouble(myText.getText());
int i = (int)d;
I have a TextField where I can type values; And I need to convert the value typed to Int and display it with S.O.Println.
EDIT:
I've tried now :
try {
int i = Integer.parseInt(myText.getText());
System.out.println("This is an integer " + i);
}
catch (NumberFormatException x) {
System.out.println("Error : "+ x.getMessage());
}
But it's giving me :
This is an integer 122 // When I type 122
Error : For input string: "122,5" // When I type 122,5
Error : For input string: "122.5" // When I type 122.5
Any ideas.
EDIT:
This is it :
double d = Double.parseDouble(myText.getText());
int i = (int)d;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看来您使用逗号作为分隔符,而不是点。无论如何,您无法将其解析为
int
。您需要Double.parseDouble(..)
使用可以使用与当前语言环境相对应的
NumberFormat
来解析输入。如果您使用 SWT(标记为 eclipse) - 我认为您可以通过向文本字段添加
VerifyListener
来限制可能的字符。在侦听器中,如果允许输入,则对传递的事件调用doit()
。如果使用 AWT(
TextField
是 awt 的文本组件) - 那么请参阅 某些内容像这样It appears you are using a comma as a separator, rather than a point. And anyway, you wouldn't be able to parse it as
int
. You'd needDouble.parseDouble(..)
Use can use
NumberFormat
corresponding to the current locale in order to parse the input.If you are using SWT (tagged eclipse) - I think you can limit the possible characters by adding a
VerifyListener
to the text field. In your listener, calldoit()
on the passed event if the input is allowed.If using AWT (
TextField
being awt's text component) - then see something like this您尝试将字符串“11,5”解析为整数。它绝对不是整数。因此抛出异常。
您可以尝试使用 Double.parseDouble() ,结果取决于您的区域设置。如果您使用英语区域设置,则需要点 (.) 而不是逗号,因此数字无论如何应该看起来像 11.5
You try to parse string "11,5" as integer. It is definitely not integer. Therefore the exception is thrown.
You can try to use Double.parseDouble() and the result depends on your Locale. If you are in English locale it expects dot (.) and not coma, so the number anyway should look like 11.5
逗号给你一个解析错误。如果需要逗号,可以将其用作分隔符,并分别解析两侧。
以下是分隔字符串中两个逗号分隔值的示例。
输出将是:
The comma is giving you a parse error. If you require the comma, you can use it as a delimiter and parse the two sides separately.
Here is an example of separating two comma separated values in a string.
The output would be:
11,5
不是 Java 无需其他交互即可解析的数字。您可以用小数点替换逗号,并删除任何空格(取决于您的区域设置):注意:
您不能使用逗号作为千位分隔符。
11,5
is not a number that Java can parse without other interaction. You can replace the comma with a decimal point, along with removing any spaces (depending on your locale):Notes:
You cannot use the comma as a thousands separator.
这是因为
myText.getText()
是11,5
(不是数字),而不是11.5
(双精度)。尝试这样做,
这会将
,
替换为.
。或者,使用
NumberFormat
进行数字转换。另外,我注意到您在解析
double
数字时使用了Integer.parseInt()
。这就是为什么你会抛出异常。您应该使用
NumberFormat.getInstance(locale).parse(myText.getText())
来获取所需的结果。不要忘记parse()
返回一个Number
对象,因此要获取双精度值,请执行以下操作:That's because the
myText.getText()
is11,5
(not numeric) and not11.5
(double).Try doing,
This replaces
,
to.
.Alternatively, uses
NumberFormat
to do your number conversion.Also, I've noticed you're using
Integer.parseInt()
when parsingdouble
numbers. That's why you're having exceptions thrown.You should use
NumberFormat.getInstance(locale).parse(myText.getText())
to get your required result. Don't forget thatparse()
returns aNumber
object, so to get a double, do this: