java判断字符串内容类型

来源:互联网 发布:智多星软件多少钱 编辑:程序博客网 时间:2024/06/06 01:30

判断字符串内容类型,是数字则打印,是字符串则输出异常。

public void value()
 {
  String str="";
        char [] chararray=str.toCharArray();
        boolean bool=true;
        int countpoint=0;
        for(int i=0;i<chararray.length;i++)
        {
         if(chararray[i]<48||chararray[i]>57)
         {
          if(chararray[i]!='.')
          {
           bool=false;
           break;
          }
          else
          {
           countpoint++;//点号的个数
          }
         }
        }
        if(bool=false||countpoint>1||(countpoint==1&&chararray.length==1))
        {
         System.out.println("异常");
        }
        else
        {
         System.out.println((int)Double.parseDouble(str));
        }
 }

原创粉丝点击