java中如何判断输入的的是String类型还是int类型的?
谢谢..

解决方案 »

  1.   


    public boolean isNumeric(String str)
    {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
       if( !isNum.matches() )
       {
          return false;
       }
       return true;
      

  2.   

    用 
    try
    {
    Integer.parseInt("your input");或者Integer.valueof("");
    //数字
    }
    catch(Exception e)
    {
     //字符
    }
      

  3.   

    或者
    import java.io.*; 
    public class Numtest { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    boolean b = true; 
    try { 
    while (b) { 
    System.out.print("请输入一个有效的整数:"); 
    String s = (String) br.readLine(); 
    for (int j = 0; j < s.length(); j++) { 
    if (!(s.charAt(j) >= 48 && s.charAt(j) <= 57)) { 
    System.out.println("您输入的不是纯数字!请重新输入."); 
    b = true; 
    break; 

    else{ 
    b = false; 


    if (!b) { 
    int i = Integer.parseInt(s); 
    System.out.println("您输入的是数字:" + i); 


    } catch (Exception e) { 
    e.printStackTrace(); 
    }  }}
      

  4.   

    你希望是什么,那就用什么类型来接受他,不可能连你自己在输入都不知道输入的什么数据吧?Scanner类中//String
    next();
    nextLine();
    //其他基本类型
    nextInt();
      

  5.   

    输入的一定是STRING类型...
    只能判断输入的字符串是否能转换成数字......
    这个应该就很简单了吧
      

  6.   

    以前写过输入一个正数(包括小数)
    public static boolean isIncludePoint(String str) {
    if (str.startsWith(".") || str.endsWith(".")) {
    return false;
    }
    int n = 0;
    for (int i = 0; i < str.length(); i++) {
    if (str.substring(i, i + 1).equals(".")) {
    n++;
    }
    }
    if (n > 1) {
    return false;
    }
    return true;
    } // 判断一个正数字是否正确,包括小数
    public static boolean IsNumber(String str) {
    String s = ".0123456789";
    if (isIncludePoint(str)) {
    for (int i = 0; i < str.length(); i++) {
    if (s.indexOf(str.substring(i, i + 1)) == -1) {
    return false;
    }
    }
    } else {
    return false;
    } return true;
    }
      

  7.   

    public static boolean isNumeric(String str){

    Pattern pattern = Pattern.compile("[0-9]*");
    Matcher isNum = pattern.matcher(str);
    if( !isNum.matches()){
    return false;
    }
    return true;

    }
      

  8.   


    public boolean isNumeric(String str)
    {
        if(str.matches("[1-9][0-9]*")){
          // 匹配ok
          return true;
        }
        return false;// 匹配error
    }
      

  9.   

    4 5楼 均可取!
    very good!
    楼主可以结贴了……
      

  10.   

    要疯了你 你就不会用instanceof嘛?Stirng name = "Mary";boolean flag = name instanceofv String;
     flag =true;
      

  11.   

    引用五楼的,如果用Integer.parseInt(String str)报错就是字符型。否则就是数字型  
    try 

    Integer.parseInt("your input");或者Integer.valueof(""); 
    //数字 

    catch(Exception e) 

     //字符 
      

  12.   

    可以用String.match("\\d+").很容易校验的。
      

  13.   

    答:我也来凑个热闹:请问:"0x1a2b"这个是不是能转成 JAVA的int类型的。你们上边哪一个做到了?