数字还是整数
最简单的办法用
try{
Double.pasreDouble(要判断的字符串);
}
catch(Exception e){
}
如果出异常就不是数字,否则就是数字

解决方案 »

  1.   

    try{
      Integer.parseInt( 你得到的输入内容 );
    }catch( Exception e ){
      不是数字,提示重输
    }
      

  2.   

    我一开始就用了这些代码可是不管用,如果输入不是数字,界面没反应:
    public boolean action(Event e,Object o)
    {
    int n1=0,n2=0,n3=0,n4=0,n,na;
    String s1,s2,s3,s4,s,sa,name;
    if(e.target.equals(b1))
    {
    try{
    s1=number2.getText();
    n1=Integer.parseInt(s1);
    s2=number3.getText();
    n2=Integer.parseInt(s2);
    s3=number4.getText();
    n3=Integer.parseInt(s3);
    s4=number5.getText();
    n4=Integer.parseInt(s4);
    }
    catch(Exception e1){
    System.out.println(e1);
    }
    n=n1+n2+n3+n4;
    s=String.valueOf(n);
    label8.setText(s);
    repaint();

    }
    是不是我的catch里面写的不恰当?谢谢了
      

  3.   

    String num;
    String pt=/([0-9]*).([0-9]*)/;
    String r=num.match(pt);
    String l=num.length;
    if(l>0)
    {
    if(r.index!=0||r.lastIndex!=l)
    {不是数字}
    }
      

  4.   

    String a num;
    double result;
    try {
        result = Double.parseDouble(num);
    }
    catch(NumberFormatException exc){
        System.out.println("Invlid number "+ num);
        return;
    }
      

  5.   

    异常没有错
    label8.setText(s);
    repaint();你试试
    label8.repaint();
      

  6.   

    谢谢各位,问题已经解决,我是这样做的:
    try{
    s1=number2.getText();
    n1=Float.parseFloat(s1);
    s2=number3.getText();
    n2=Float.parseFloat(s2);
    s3=number4.getText();
    n3=Float.parseFloat(s3);
    s4=number5.getText();
    n4=Float.parseFloat(s4);

    }
    catch(NumberFormatException e1){

    JOptionPane.showMessageDialog(null,"请输入数字!") ;
    }看来catch语句没有错,但是在图形界面里system.out.println没有反应,只好用了joptionpane
    当然为此还要加入import javax.swing.*;
    这样就行了。谢谢大家,互相学习!