您的位置:首页 > 其它

test if a string is an integer?

2010-09-19 20:01 453 查看
http://bytes.com/topic/java/answers/18107-test-if-string-integer



"dave" <go_away_you_spammer_scum@nowhere.com> wrote in message
news:FAHJc.59786$Xb4.4468@nwrdny02.gnilink.net...[color=blue]
> I am reading input from a form. I want to validate the input by making[/color]
sure[color=blue]
> 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.
>[/color]

Try to perform Integer.parseInt(yourString) and if it throws a
NumberFormatException you'll know the string isn't a valid integer



public Boolean isInt10(String int10)
	{
		try
		{
			Integer.parseInt(int10);
		}
		catch(NumberFormatException ex)
		{
			return false;
		}

		return true;
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: