检查,看看一个字符串是一个整数 - 的Java [英] Checking to see if a string is an integer - Java

查看:110
本文介绍了检查,看看一个字符串是一个整数 - 的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java\">Determine如果字符串是Java中的整数


我发现有关我的问题的几个环节,但他们不正是我一直在寻找。我想preFER使用一个简单的布尔比学会使用try / catch语句的事情。不管怎么说,这里是我的问题:

我想设置一个布尔值true或false取决于如果一个字符串是一个整数> = 10和&lt; = 100。因此,我的目标是为布尔答案:

 字符串的垃圾;
如果(垃圾是10和100之间的整数){
返回true;}
其他{
返回false;}


 如果垃圾= 50,它的真实。
如果垃圾=五十年代,它的假。
如果垃圾= 50:磷,其假。
如果垃圾= 5,它的假。
如果垃圾= 500,其假。

谢谢!


解决方案

 静态布尔isIntegerBetween10And100(字符串垃圾){
     尝试{
        INT X =的Integer.parseInt(垃圾);
        返回X&GT; = 10安培;&安培; X'LT; = 100;
     }
     赶上(NumberFormatException的E){
        返回false;
     }
  }

Possible Duplicate:
Determine if a String is an Integer in Java

I have found several links related to my question, but they were not exactly what I was looking for. I would prefer to use a simple boolean than to learn to use the try/catch thing. Anyways, here is my question:

I want to set a boolean to true or false depending on if a string is an integer >= 10 and <= 100. So my objective is to set the boolean "answer":

String junk;
if (junk is an integer between 10 and 100){
return true;}
else{
return false;}


if junk = 50, its true.
if junk = fifty, its false.
if junk = 50:-p, its false.
if junk = 5, its false.
if junk = 500, its false.

Thanks!

解决方案

 static boolean isIntegerBetween10And100(String junk){
     try{
        int x = Integer.parseInt(junk);
        return x >= 10 && x <= 100;
     }
     catch(NumberFormatException e){
        return false;
     }
  }

这篇关于检查,看看一个字符串是一个整数 - 的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆