The following code parses a String into String Objects and places the values in an Object array.
For any strings that represent a number it then attempts to replace the String with a Double object. However I get the following Exception:
java.lang.ArrayStoreException: java.lang.Double
public class Main
{
public static void main(String[] args) throws Exception
{
String text = "one,two,123,123.45";
Object[] row = text.split(",");
for (int i = 0; i < row.length; i++)
{
try
{
row[i] = new Double( row[i].toString() );
}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println();
for(Object o: row)
System.out.println(o.getClass());
// This works, I don't see the difference?
Object[] data = new Object[2];
data[0] = "one";
data[1] = "111.22";
data[1] = new Double( data[1].toString() );
System.out.println();
for(Object o: data)
System.out.println(o.getClass());
}
}
Any idea what I'm doing wrong? Why does the second case work but not the first?