a到底是String还是int啊,两者是不一样的
b又说是字符串,怎么突然就说也可以是Object??将Object转换成String,可以直接b.toString()就可以了
将String转换成int,可以用Integer.parseInt("123");得到

解决方案 »

  1.   

    楼主表达不清.
    a既然是字符串又怎么会是 int,字符串就是String类型呀.
    b也是一个字符串,那也是一个String类型,又怎么会是Object?
    你把你的程序贴上来,需要转换什么说清楚呀:)
      

  2.   

    我是楼主。现从新更正。
    String a 的值是 “String” 或 “int” 。他的值是动态从数据库取得的。表示一个字段的java对应类型;
    String b 的值是从HashMap中得到后作的类型转换;
    我现在要将 b 的类型转换成 a 的值所表示的类型;
      

  3.   


    if(a.equals("String"))
        b 不变,还是String类型;
    else
        b = Integer.parseInt();
      

  4.   

    哈哈,你最好把int换成Integer,你看我的做法满意不?String a = yourType;   //yourType为String或Integer
    Object b = hm.get("567");
    HashMap hm = yourHM;   //
    if(a.equals("Integer")){  b= (Integer)b;
    }
    if(a.equals("String")){
      b= (String)b;
    }//没有进行边缘检查,我设定你的Hm和a的值就是正确对应的了
      

  5.   

    不知道你碰到的是不是这个问题,int型无法put进hashmap,如果是用
    hashmap.put(key,new Integer(int))就ok了
      

  6.   

    c 是一个对象。
    b=c.toString();
    要作的是
    d = (a)b;
      

  7.   

    a 是从数据库得到的字段的类型。我不想弄二十几个if
      

  8.   

    package untitled1;import java.util.*;public class TestType {
        private String a = null;
        private String b = null;
        private HashMap hashMap = null;    public TestType() {
            hashMap = new HashMap();
            hashMap.put("temp1", "I am String");
            hashMap.put("temp2", "12345");
            //while (true) {
                Random rd = new Random();
                int c = rd.nextInt(2);
                if (c == 0)
                    a = "String";
                else if (c == 1)
                    a = "int";
                else
                    a = "NULL";
            //}
        }    public static void main(String[] args) {
            TestType tt = new TestType();
            if (tt.a.equals("String")) {
                tt.b = (String)tt.hashMap.get("temp1");
                String finalB = tt.b;
                System.out.println("String: "+tt.b);
            }
            else if (tt.a.equals("int")) {
                tt.b = (String)tt.hashMap.get("temp2");
                int finalB = Integer.parseInt(tt.b);
                System.out.println("int: "+finalB);
            }
            else
                System.out.println("I don't konw!");
        }
    }