Failed to store integer in object array
I want to parse a String from Object [] to Integer and store it in the same place like this:
public class ArrParseTest {
public static void main(String[] args) {
Object[] arr = new Object[2];
String input = "Boo;Foo;1000";
Integer somInt = new Integer(0);
arr = input.split(";", -1);
somInt = Integer.parseInt((String) arr[2]);
arr[2] = somInt;
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
but i am getting all this exception:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
at main.ArrParseTest.main(ArrParseTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
I don't understand why I can't just store this parsed object into an array, although I
Object [] arr = new object [2];
accurately designed to store various objects in an array.
Anyone now how can I parse this string to Integer and store it in an array?
source to share
This is the problem causing the immediate problem you are seeing:
arr = input.split(";", -1);
You are assigning a reference to a type object to a type String[]
variable Object[]
. This is fine, but it means that you cannot store non-string references in an array.
You probably want:
String input = "Boo;Foo;1000";
Integer someInt = new Integer(0);
String[] split = input.split(";", -1);
Object[] arr = new Object[split.length];
System.arraycopy(split, 0, arr, 0, split.length);
This will copy the content String[]
at Object[]
the same size. Then you can assign a new value to any element Object[]
, and that new value can be a reference Integer
.
It's not clear why you are initializing with a someInt
value that you ignore, by the way. Indeed, you don't even need a variable:
arr[2] = Integer.valueOf((String) arr[2]);
or
arr[2] = Integer.valueOf(split[2]);
source to share
ArrayStoreException thrown to indicate that an attempt was made to store the wrong type of object in an array of objects.
Object[] arr = new Object[2];
String input = "Boo;Foo;1000";
Integer somInt = new Integer(0);
arr = input.split(";", -1);
somInt = Integer.parseInt(arr[2].toString());
arr[2] = ""+somInt; //storing non string value will give error
for (int i = 0; i < arr.length; i++)
{
System.out.println(arr[i]);
}
source to share
ArrayStoreException
is a runtime exception that is thrown when the type of the object you are adding to the array is different from the type of the object that must be an element of the array.
In your code, when you define somInt
before, the arr[2]
array contains objects of the type String
, whereas someInt
- Integer
. You can assign String
as follows:
arr[2] = "" + somInt;
source to share
split
method String
returns an array of typeString
and you are trying to store an integer value in that type array String
in a string:
arr[2] = somInt;
you don't get compiler error like String
extends Object
. But you get a runtime error because after execution arr
it becomes an array of type String
.