-4

I know this question sounds stupid for several members here, but i have no idea anymore.

I faced a strange problem with array. I try to get the index 1 on ArrayList but it always throws java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 .

I have debug by the following code :

for (int o=0; o<vcard.size(); o++){
    if(vcard.get(o).contains(":")){
        System.out.println("Contains");
        String[] splitByTitikDuaVCard = vcard.get(o).split(Pattern.quote(":"));
        //this line show the length is 2 
        System.out.println("RESULT LENGTH : " + splitByTitikDuaVCard.length );
        for (int y=0; y<splitByTitikDuaVCard.length; y++){
            System.out.println("RESULT Y : " + splitByTitikDuaVCard[y] + ", INDEX : " + y);
            mapContentTerbaruVCard.put(splitByTitikDuaVCard[0], splitByTitikDuaVCard[1]);
        }
    }else{
        System.out.println("Not Contains");
    }
}

The result of the log :

03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : BEGIN, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : VCARD, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : VERSION, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : 3.0, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : N, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : Doe;John, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : FN, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : John Doe, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : ORG, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : PT. Pertamina, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TITLE, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : Bussiness Manager, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : ADR, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : ;;Soekarno Hatta;Bandung;;46153;West Jave, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TEL;WORK;VOICE, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : 021 203312, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TEL;CELL, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : +62818456463111, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TEL;FAX, INDEX : 0

The log show that the index 1 is exist, but when i try to put the index 1 into HashMap, the error java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 is appears

My Question is : How to find the index 1 so i can save it into hashmap

Note : i have try this (Java ArrayList IndexOutOfBoundsException Index: 1, Size: 1) solution but not working on my case.

Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Koma
  • 1
  • 2
  • 5
    The error tells you that the size of the ArrayList is 1. The index of the only element is therefore 0. – Code-Apprentice Mar 08 '17 at 14:45
  • "The log show that the index 1 is exist" Please show this log. Also, you should give a complete code example. Be sure to include put your code inside a method which is inside a class. Finally, post the entire stacktrace and show us which line causes the error. – Code-Apprentice Mar 08 '17 at 14:46
  • Arrays start counting from 0. Basically what the error message tells you is the following: You have an array/arraylist which has 1 element and you tried to access the element at index 1 which is actually the second element in your list. (or is not since you got an error). – Andrei Olar Mar 08 '17 at 14:47
  • Saying "i have try this..." is not sufficient. Your code will never be exactly the same as the code given in the link. This means you need to show us **exactly** what you tried so that we can help you from there. – Code-Apprentice Mar 08 '17 at 14:48
  • @code-Apprentice : in case, i have split the string by `:`. it should be contains two index = `index 0` and `index 1`. correct me please – Koma Mar 08 '17 at 14:49
  • all : i have posted the result of the log – Koma Mar 08 '17 at 14:53
  • See my updated answer and you'll understand. – LppEdd Mar 08 '17 at 14:57
  • 1
    See my second comment above. – Code-Apprentice Mar 08 '17 at 15:03
  • i have updated my post, sir. please check it. – Koma Mar 08 '17 at 15:05
  • Look closely at your log. – LppEdd Mar 08 '17 at 15:43

2 Answers2

0

splitByTitikDuaVCard may have lenght = 1, so position [1] is out of its bounds.

For OP, try it yourself. You'll notice the length is 1.

public class Test
{
   public static void main(final String[] args) {
      final String[] splitted = "String:".split(":");
      System.out.println(splitted.length);
   }
}

Lookint at your log:

03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 1

length = 1

LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • but i have split it by `:`, it should be contains 2 index therefore `index 0` and `index 1`, please corrent me. – Koma Mar 08 '17 at 14:48
  • @Koma See updated answer, it will explain what may happen. – LppEdd Mar 08 '17 at 14:52
  • no sir. i do `String[] splitByTitikDuaVCard = vcard.get(o).split(Pattern.quote(":"));` and `System.out.println("RESULT LENGTH : " + splitByTitikDuaVCard.length );` the length show `2` – Koma Mar 08 '17 at 15:01
  • It's impossible. Post the stacktrace. – LppEdd Mar 08 '17 at 15:02
  • See updated answer. Last record of your log shows length = 1. So my guess is right. – LppEdd Mar 08 '17 at 15:06
-1

In Second Loop (Inner loop) you are getting value from splitByTitikDuaVCard[1] why you are putting value of indexes hardcoded. you should use the value of y.

Replace this

mapContentTerbaruVCard.put(splitByTitikDuaVCard[0], splitByTitikDuaVCard[1]);

With this

mapContentTerbaruVCard.put("key"+y, splitByTitikDuaVCard[y]);

in this code your key will be key0, key1, ....

Try this code it will help you

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • what exactly "key" means?. I my case i need to save the `index 0` of `splitByTitikDuaVCard` as the `key` into `hasmap` – Koma Mar 08 '17 at 14:57
  • **key** is the identification for value in `HashMap`. You don't know the **value** in `HashMap` but you know the key, then you can easily found the value of specific **key**. And yes store index as a key so you can get value from index. But do not store **hard-coded** value. see my answer again. – Vishal Chhodwani Mar 09 '17 at 06:11