Puki
Puki

Reputation: 157

Java substring method giving IndexOutOfBoundsException

I am trying to separate String [] adr = {"Tel Iva +38712345", "Mail Iva [email protected]", "Tel Ana +12345678"} by looking at each of the element's first word. If the first word is Mail, it goes to String [] m, and if the first word is Tel, it goes to String [] t.

Here is my code:

public static void rep(String a, String []adr) {

    int mail=0, tel=0;
    for (int i=0; i<adr.length; i++) {
        if(adr[i].substring(0, 3).equals("Mail")) {
            mail++;
        }
        else tel++;
    }
    String [] m = new String [mail];
    String [] t = new String [tel];

    for(int i=0; i<adr.length; i++) {
        if(adr[i].substring(0, 4).equals("Mail")) {
            m[i]=adr[i].substring(5);
        }

        else t[i]=adr[i].substring(4);
        System.out.println(adr[i].substring(0, 4));
    }
}

But for some reason unknown to me, I get

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

which points at line m[i]=adr[i].substring(5). I really do not understand why. Any help would be appreciated.

Upvotes: 1

Views: 769

Answers (4)

Samir
Samir

Reputation: 36

Correct Solution. you need to two indices to track m and t array traversal.

public static void rep(String a, String []adr) {

    int mail=0, tel=0;
    for (int i=0; i<adr.length; i++)
    {
        if(adr[i].substring(0, 4).equals("Mail")) {
            mail++;
        }
        else tel++;
    }
    String [] m = new String [mail];
    String [] t = new String [tel];
    int mIndex =0, tIndex = 0;

    for(int i=0; i<adr.length; i++) {
        if(adr[i].substring(0, 4).equals("Mail")) {
            m[mIndex]=adr[i].substring(4);
            mIndex++;
        }
        else
        {
            t[tIndex]=adr[i].substring(4);
            tIndex++;
        }
        System.out.println(adr[i].substring(0, 4));
    }
}

Upvotes: 2

VietDD
VietDD

Reputation: 1148

Supposedly index you used in the substring method are correct and I will only talk about index of the array :

String [] adr = {"Tel Iva +38712345", "Mail Iva [email protected]", "Tel Ana +12345678"}

By this data,

mail array's size will be 1, max index can use for mail array is 0

tel array's size will be 2, max index can use for tel array is 1

for(int i=0; i<adr.length; i++) {
    if(adr[i].substring(0, 4).equals("Mail")) {
        m[i]=adr[i].substring(5);
    }

    else t[i]=adr[i].substring(4);
    System.out.println(adr[i].substring(0, 4));
}

In this loop :

LOOP 1 : i = 0 -> t[0]=xxx; -> OK

LOOP 2 : i = 1 -> m[1]=xxx; -> ERROR, because size of m array is 1, index can only be 0

PS : you need to check the index used in substring method

Upvotes: 1

Chamly Idunil
Chamly Idunil

Reputation: 1872

suppose this sample address array (your parameter adr[])

adr[0] = [email protected]
adr[1] = Tele123456
adr[2] = [email protected]
adr[3] = Tele123456
adr[4] = [email protected]

After your first loop which is assign values to int mail and tel

mail = 3;
tel = 2

so your m and t array looks like below.

    String [] m = new String [3];
    String [] t = new String [2];

What happen is in your last for looping for adr (you parameter array) length which is 5.

and try to assign values to m or t, by index of adr array.

Ex : adr[3] = Tele123456

on your second loop when i = 3 your getting this value and try to assign this value to

t[3] = 123456

where actually t size is 2 and then it occur array out of bound exception.

Hope you understood the issue on your code.

Rather than array for m and t use List.

Consider below example.

public static void rep(String a, String []adr) {

    List<String> mails = new ArrayList<>();
    List<String> teles = new ArrayList<>();
    for (int i=0; i<adr.length; i++) {
        if(adr[i].substring(0, 3).equals("Mail")) {
            mails.add(adr[i].substring(5));
        } else {
           mails.add(adr[i].substring(5));
        }
    }
}

**Note : please fix compile errors if there.

Upvotes: 0

Zaid Khan
Zaid Khan

Reputation: 367

Well You can try this method, I once coded same type of problem when I was learning Java for the my academics. Well you can also try StringTokenizer method to do the same. Maybe they works better. I am expecting that you are going to insert the whole string not splitted one.

import java.util.*;
import java.lang.*;
import java.*;

public class stringtoken{


  public static void main(String args[]){


         List<String> m=new ArrayList<String>();
         List<String> t=new ArrayList<String>();

         String[] s={"Tel Iva +38712345", "Mail Iva [email protected]", "Tel Ana +12345678"};

         for(int i=0;i<s.length;i++){

                 if(s[i].indexOf("Tel")==0){

                       t.add(s[i]);
                 }
                 else if(s[i].indexOf("Mail")==0){
                       m.add(s[i]);
                 }
         }

         for(int i=0;i<m.size();i++){
                System.out.println(m.get(i));
         }
         for(int i=0;i<t.size();i++){
               System.out.println(t.get(i));
         }
     }
 }

Upvotes: 1

Related Questions