测试字符串是否为整数? [英] test if a string is an integer?
问题描述
我正在阅读表格中的输入。我想验证输入是否确认
该字符串实际上是一个整数。我该怎么做?我是否需要
将其转换为角色阵列并分解每个角色并进行测试?
还是有更简单的方法吗?谢谢。
---
外发邮件经过认证无病毒。
由AVG反病毒系统检查( http://www.grisoft.com)。
版本:6.0.720 /病毒库:476 - 发布日期:2004年4月14日
" dave" <去********************** @ nowhere.com>在留言中写道
新闻:FA ****************** @ nwrdny02.gnilink.net ...我我正在阅读表格中的输入。我想通过使
确保该字符串实际上是一个整数来验证输入。我该怎么做?我是否需要将其转换为角色阵列并分解每个角色并对其进行测试?
还是有更简单的方法?谢谢。
尝试执行Integer.parseInt(yourString),如果它抛出一个
NumberFormatException,你就会知道字符串isn' '有效整数
" dave" <去********************** @ nowhere.com>在留言中写道
新闻:FA ****************** @ nwrdny02.gnilink.net ...我我正在阅读表格中的输入。我想通过使
确保该字符串实际上是一个整数来验证输入。我该怎么做?我是否需要将其转换为角色阵列并分解每个角色并对其进行测试?
还是有更简单的方法?谢谢。
将字符串传递给parseInt并捕获NumberFormatException。
参见:
http://java.sun .com / j2se / 1.4.2 / docs / ... va.lang.String )
-
Gary
" Murray" < PA *** @ SMAFFoffSPAMMER.optusnet.SPAMMAGE.com.au>在消息中写道
新闻:6P **************** @ news-server.bigpond.net.au ...
dave <去********************** @ nowhere.com>在消息中写道
新闻:FA ****************** @nwrdny02.gnilink.net ...我正在阅读来自表单。我想通过使确定
来验证输入,该字符串实际上是一个整数。我该怎么做?我需要
将其转换为字符数组并分解每个字符并测试
吗?或者有更简单的方法吗?谢谢。
尝试执行Integer.parseInt(yourString),如果它抛出一个
NumberFormatException,你就会知道字符串isn''ta有效整数
你真的是指整数吗?或者是数字。好的,那么你可以使用
Double.parseDouble(string);这将返回整数的结果,
浮点数和双打。
I am reading input from a form. I want to validate the input by making sure
that the string is actually an integer. How would I do this? Do i need to
convert it to a character array and break down each character and test it?
or is there an easier way? Thanks.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.720 / Virus Database: 476 - Release Date: 7/14/04
"dave" <go**********************@nowhere.com> wrote in message
news:FA******************@nwrdny02.gnilink.net...I am reading input from a form. I want to validate the input by making sure that the string is actually an integer. How would I do this? Do i need to
convert it to a character array and break down each character and test it?
or is there an easier way? Thanks.
Try to perform Integer.parseInt(yourString) and if it throws a
NumberFormatException you''ll know the string isn''t a valid integer
"dave" <go**********************@nowhere.com> wrote in message
news:FA******************@nwrdny02.gnilink.net...I am reading input from a form. I want to validate the input by making sure that the string is actually an integer. How would I do this? Do i need to
convert it to a character array and break down each character and test it?
or is there an easier way? Thanks.
Hand the String to parseInt and catch NumberFormatException.
see:
http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
--
Gary
"Murray" <pa***@SMAFFoffSPAMMER.optusnet.SPAMMAGE.com.au> wrote in message
news:6P****************@news-server.bigpond.net.au...
"dave" <go**********************@nowhere.com> wrote in message
news:FA******************@nwrdny02.gnilink.net...I am reading input from a form. I want to validate the input by making
surethat the string is actually an integer. How would I do this? Do i need to convert it to a character array and break down each character and test it? or is there an easier way? Thanks.
Try to perform Integer.parseInt(yourString) and if it throws a
NumberFormatException you''ll know the string isn''t a valid integer
Do you really mean integer? Or is a "number" ok, then you can use
Double.parseDouble(string); which will return a result for integers,
floats, and doubles.
这篇关于测试字符串是否为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!