将 TextField 值转换为 Int

发布于 2024-10-12 23:28:03 字数 711 浏览 6 评论 0原文

我有一个文本字段,我可以在其中输入值;我需要将键入的值转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜未央樱花落 2024-10-19 23:28:03

看来您使用逗号作为分隔符,而不是点。无论如何,您无法将其解析为 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 need Double.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, call doit() on the passed event if the input is allowed.

If using AWT (TextField being awt's text component) - then see something like this

浪菊怪哟 2024-10-19 23:28:03

您尝试将字符串“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

故笙诉离歌 2024-10-19 23:28:03

逗号给你一个解析错误。如果需要逗号,可以将其用作分隔符,并分别解析两侧。

以下是分隔字符串中两个逗号分隔值的示例。

String str = "1,2,3,4";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
}

输出将是:

1
2
3
4

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.

String str = "1,2,3,4";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
}

The output would be:

1
2
3
4
冷情 2024-10-19 23:28:03

11,5 不是 Java 无需其他交互即可解析的数字。您可以用小数点替换逗号,并删除任何空格(取决于您的区域设置):

Double.parseDouble(myText.getText().replaceAll(",", ".").replaceAll(" ", ""));

注意:

您不能使用逗号作为千位分隔符。

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):

Double.parseDouble(myText.getText().replaceAll(",", ".").replaceAll(" ", ""));

Notes:

You cannot use the comma as a thousands separator.

月下凄凉 2024-10-19 23:28:03

这是因为 myText.getText()11,5 (不是数字),而不是 11.5 (双精度)。

尝试这样做,

Double.parseDouble(myText.getText().replaceAll(",", "."));

这会将 , 替换为 .

或者,使用 NumberFormat 进行数字转换。


另外,我注意到您在解析double数字时使用了Integer.parseInt()。这就是为什么你会抛出异常。

您应该使用 NumberFormat.getInstance(locale).parse(myText.getText()) 来获取所需的结果。不要忘记 parse() 返回一个 Number 对象,因此要获取双精度值,请执行以下操作:

double d = NumberFormat.getInstance(locale).parse(myText.getText()).doubleValue();

That's because the myText.getText() is 11,5 (not numeric) and not 11.5 (double).

Try doing,

Double.parseDouble(myText.getText().replaceAll(",", "."));

This replaces , to ..

Alternatively, uses NumberFormat to do your number conversion.


Also, I've noticed you're using Integer.parseInt() when parsing double 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 that parse() returns a Number object, so to get a double, do this:

double d = NumberFormat.getInstance(locale).parse(myText.getText()).doubleValue();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文