(Java)获取加载到动态类型对象中的字符串值?

(Java)获取加载到动态类型对象中的字符串值?,java,string,Java,String,我对Java非常陌生(大约10天),所以我的代码可能非常糟糕,但我得到的是: ArgsDataHolder argsData = new ArgsDataHolder(); // a class that holds two // ArrayList's where each element // r

我对Java非常陌生(大约10天),所以我的代码可能非常糟糕,但我得到的是:

ArgsDataHolder argsData = new ArgsDataHolder();  // a class that holds two
                                                 // ArrayList's where each element
                                                 // representing key/value args
Class thisArgClass;
String thisArgString;
Object thisArg;

for(int i=2; i< argsString.length; i++) {
    thisToken = argsString[i];
    thisArgClassString = getClassStringFromToken(thisToken).toLowerCase();
    System.out.println("thisArgClassString: " + thisArgClassString);
    thisArgClass = getClassFromClassString(thisArgClassString);

    // find closing tag; concatenate middle
    Integer j = new Integer(i+1);
    thisArgString = getArgValue(argsString, j, "</" + thisArgClassString + ">");

    thisArg = thisArgClass.newInstance();
    thisArg = thisArgClass.valueOf(thisArgString);
    argsData.append(thisArg, thisArgClass);
}
ArgsDataHolder argsData=new ArgsDataHolder();//容纳两个学生的班级
//ArrayList是每个元素所在的位置
//表示键/值参数
类thisArgClass;
字符串thisArgString;
对象thisArg;
for(int i=2;i
用户基本上必须以以下格式在命令提示符中输入一组键/值参数:
value
,例如
62
。在本例中,
thisArgClass
将等于
Integer.class
thisArgString
将是读取“62”的字符串,
thisArg
将是等于62的整数实例

我尝试了
thisArg.valueOf(thisArgString)
,但我猜
valueOf()
只是对象某些子类的一种方法。无论出于何种原因,我似乎无法将thisArg强制转换为thisArgClass(例如:
thisArg=(thisArgClass)thisArgClass.newInstance();
,此时应该可以访问
valueOf()

必须有一个好的、干净的方法来实现这一点,但这超出了我的能力。我如何才能将字符串的值加载到动态类型的对象(整数、长、浮点、双精度、字符串、字符、布尔等)?或者我只是想得太多了,Java会为我做转换吗?:困惑:

我似乎无法将thisArg强制转换为thisArgClass(例如:thisArg=(thisArgClass)thisArgClass.newInstance()

这将不会像这样工作,因为您需要先初始化
thisArgClass
。这将产生编译时错误。更改代码如下:

Class thisArgClass = null;
try {
    Object thisArg = thisArgClass.newInstance();
} catch (InstantiationException ex) {
    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
}

希望这能对您有所帮助。

这里有几处错误。我假设
thisArgClass
已正确设置;例如,它将包含
Integer.class
。以便调用
newInstance()
对象上,该类必须具有无参数构造函数。类
整数
没有此类构造函数,因此必须使用更迂回的方法调用一个现有构造函数:

Constructor<Object> c = thisArgClass.getConstructor(String.class);
Object i = c.newInstance(thisArgString);
Constructor c=thisArgClass.getConstructor(String.class);
对象i=c.newInstance(thisArgString);

由于直到运行时才知道对象的实际类型,因此必须使用
并在使用值之前将结果强制转换为所需的类型。

[主题外]:类应使用UppercaseStartingCamelCase命名(因此
argsDataHolder
应为
argsDataHolder
)。在哪个类上使用静态方法“装箱”对象(整型、长型、布尔型)等具有
valueOf()
方法,但它们(a)是静态的,并且(b)没有共同的ancestory。您必须反射性地调用该方法,而不是
thisArgClass.valueOf(thisArgString)
。您有两行连续的行将值分配给
thisArg
——这是“输入错误”吗“?Michael,如果你知道右边的变量类型是整数、Long、Float、Double、String等等,那么thisArg=(String)thisArgClass就可以了。如果你不知道,那么你必须使用--@vajapravin的实例:你能把它写下来作为答案吗(更详细地说),因为我不理解你的评论。对不起……代码中的
应该表示变量赋值的部分。我将添加它以使其更清楚。@Michael OK,这样其他人也会更清楚,得到答案的机会也会更大。我之前尝试过类似的方法,但它没有dn不起作用。我想我传递的getConstructor()参数类型错误。请稍后在此处尝试您的解决方案。