-3

I am facing java.lang.ArrayIndexOutOfBoundsException.... But my String includes more than 2 values

  String[] elements = { "Allama Iqbal","born","in","Sailkot"};

 String str = String.join(",", elements); 

           String str = String.join(",", elements);
String[] strArray = new String[] {str};
          if(strArray[2].equals("NNP") ) {
}

The POS are...Allama//NNP
The POS are...Iqbal//NNP
The POS are...,//,
The POS are...born//VBN
The POS are...,//,
The POS are...in//IN
The POS are...,//,
The POS are...Sailkot//NNP
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
imran fida
  • 15
  • 1
  • 6

1 Answers1

2

This

String str = String.join(",", elements);

Returns a single string. Therefore this

String[] strArray = new String[] {str};

only has 1 element

Mike
  • 4,722
  • 1
  • 27
  • 40
  • so what shouuld i do – imran fida Feb 07 '19 at 19:14
  • 3
    You haven't provided any details on what your code is _supposed_ to do so I can't really say. I would update the question to include details on what the task your application is trying to accomplish. – Mike Feb 07 '19 at 19:22