ArrayIndexOutOfBoundsException error in parsing JSON data - java

I am getting ArrayIndexOutOfBoundsException at id[i] = c.getString(TAG_ID);
Here's the code:
for (int i = 0; i < 25; i++) {
JSONObject c = contacts.getJSONObject(i);
id[i] = c.getString(TAG_ID);
}
I have checked that JSON file contains 25 objects. I have also tried using i<10 but it still gives same error.

id should be an array with at least 25 elements to avoid index out-of-bound.
String [] id = new String[25];

Initialize id[] array equivalent to loop condition before entering the for loop.
Or
Add null check to array id[] size inside the for loop and Initialize array equivalent to loop condition.

You declared your id array as -
String id[] = {null};
That is size of your id array is 1. When you are trying to access 25th or 10th array you are getting the ArrayIndexOutOfBoundException.
Redefining your id array may help you -
String[] id = new String[25];
Or better you may use ArrayList then you don't have to think about the size of the array -
List<String> id = new ArrayList<String>(); //declaration of id ArrayList
Then in your for loop you may do this -
for (int i = 0; i < 25; i++) {
JSONObject c = contacts.getJSONObject(i);
id.add(c.getString(TAG_ID));
}
Hope it will Help.
Thanks a lot.

Related

How can I convert a Jsoup Document[] array to a String[]?

My question is the same as this one except that instead of a single Document I have an array (Document[]).
I normally use R, not Java, so I apologize if it should be apparent how to change the solution from the linked thread for the case of an array.
The solution for the case of a single Document object was:
String htmlString = doc.html();
My code to create the object was:
Document[] target = new Document[20];
for(int n=0; n < strvec.length;n++){
target[n] = Jsoup.connect(strvec[n]).get();
}
I tried a few things like creating the original target object as String[], putting .toString() on the end of Jsoup.connect(strvec[n]).get() and elsewhere, but these attempts were unsucessful.
it is assumed that serves is an array of String containing the URL to connect, you do not need to create another array of Document
String[] result = new String[strvec.length];
for(int n=0; n < strvec.length;n++)
result[n]=Jsoup.connect(strvec[n]).get().html();
String[] htmlList = new String[target.length];
for(int i = 0; i < target.length; i++)
htmlList[i] = target[i].html();
This loop should do what you want.

Adding elements dynamically in String array but index 0 showing null?

I want to add some values in string array dynamically by using for loop.
when i debug the code at first for loop it is showing values which are adding.I want to check if any one of the string equals to my given value.but in for loop 2 at index 0 it showing null and at index 1 it showing the string value.
for(int i=0;i<someval;i++) {
String[] mylist = new String[someval];
mylist[i]=previousVal;
System.out.println("Previous Value : " +mylist[i]);
}
for (int j = 0; j <=mylist.length; j++) {
if (mylist[j].equals(givenValue) ) { {
System.out.println("your value found in the array");
}
}
The problem is that you are creating a new array using
mylist = new String...
during each loop iteration.
So, it doesn't really matter if you write something to an array, if the next step consists of throwing that array away.
In other words: make sure that you create the array just once; preferable before entering your loop.

Java String treatment with splitting

I need your help. I have a string with value "1,2,3,4,5,6,7,8,9,10".
What I want to do is take once first value ("1") only (substring (0, 1) for example) and then do a loop with the rest of values except the first value that I already take.
Maybe I have to create another String variable and set the values without first value to the second String variable and then create a loop? How to do that?
The easiest way would probably be to use String#split(String):
String str = "1,2,3,4,5,6,7,8,9,10";
String[] parts = str.split(",");
// Save the first part
String firstPart = parts[0];
// Iterate over the others:
for (int i = 1; i < parts.length; ++i) {
System.out.println (parts[i]); // Or do something useful with it
}
You can use split function.
String numbers = "1,2,3,4,5,6,7,8,9,10"; //Here your String
String[] array = numbers.split(","); //Here you divide the String taking as reference the ,
String number = array[0] //You will get the number 1
If you want to take the rest of the elements:
for(i = 1; i < array.length; i++)
System.out.println(array[i]);
I expect it will be helpful for you!

How do you determine size of string array java?

How do i read a file and determine the # of array elements without having to look at the text file itself?
String temp = fileScan.toString();
String[] tokens = temp.split("[\n]+");
// numArrayElements = ?
Use the length property of the array:
int numArrayElements = tokens.length;
The proper expression is tokens.length. So, you can assign numArrayElements like this:
int numArrayElements = tokens.length;
This counts the number of elements in the tokens array. You can count the number of elements in any array in the same way.

How can I store a string Matrix inside an array?

I have to store a String matrix(3x20) inside an array whose length may vary.
I am trying the following code but I am getting an incompatible types error.
How could I fix this error?
My code is:
int x=0;
String[] arrayF=new String[10];
arrayF[x]= new String[3][20];
You can't assign array this way. You should eventually assign each element of the first 2-array to the 1-d array.
Something like:
String[][] array2D =new String[M][N];
String[] array1D = new String[M * N];
for (int i = 0 ; i < M ; i++)
{
for (int j = 0 ; i < N ; i++)
{
array1D[(j * N) + i] = array2D[i][j];
}
}
arrayF is an array of strings, so each element in arrayF must be a string (by definition of the array).
What you are trying to do is is put an array (new String[3][20]), instead of a string, in each element of arrayF, which obviously contradicts it's definition (hence the incompatible types error).
One solution for what you want might be using a 3-d array of strings:
String[][][] arr = new String[10][3][20];
arrayF is one dimensional array with String type.
You cannot add two dimensional array to arrayF. For dynamic array size, you should use ArrayList.
List<String[][]> main = new ArrayList<String[][]>();
String[][] child1 = new String[3][20];
String[][] child2 = new String[3][20];
main.add(child1);
main.add(child2);
Refer to
Variable length (Dynamic) Arrays in Java
use something like this:
String [][] strArr = new String[3][20];
ArrayList<String[][]> tm = new ArrayList<String[][]>();
tm.add(strArr);

Categories