-1

For some reason that I don't understand, I'm getting a array out of bounds at

String ans1 = inputs[1]; The purpose of this code is to add questions and answers to 2 different array by splitting a single input string into multiple parts and add them to the input and output array respectively. I know what a array out of bounds is, I just dont understand why split is not working properly.

sl=tf.getText();     
boolean tempsave = false;
if (sl.equalsIgnoreCase("!tempadmin"))
{
int ends = sIndata.length;
int endss = resdata.length;
int endends =0;
int endendss =0;
    ta.append("adding options\n");
    do{
       String sp = tf.getText();
       if (sl.equalsIgnoreCase("kill"))
               {
                   tempsave = true;
               };
        String [] inputs = sp.split(";");
        String qs1 = inputs[0];
        String ans1 = inputs[1];
        String[] qs2= qs1.split(":");
        String[] ans2= ans1.split(":");
        for (int qusilada = 0; qusilada != qs2.length; qusilada++){
        sIndata[ends + 1][endends + qusilada] = qs2[qusilada];
        resdata[endss + 1][endendss + qusilada] = ans2[qusilada];        
                ends++;
                endss++;
       }
                sp = "";
    }while (tempsave == false);
    ta.append("tempadmin closed");
};
Nonsouris
  • 3
  • 3
  • May be your `sp` did not contain any value or did not contains `;` – Blasanka Jul 15 '17 at 04:38
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Blasanka Jul 15 '17 at 04:55

1 Answers1

0

Check array length before calculating index values :

String [] inputs = sp.split(";");
    String qs1 = "";
    String ans1 = "";
    if(inputs.length>1){
       qs1 = inputs[0];
       ans1 = inputs[1];
    }
Mohit Tyagi
  • 2,594
  • 3
  • 14
  • 29