It is necessary to divide into words, tried to use \s , but divides the line into only 2 words, and the rest cause an exception ArrayIndexOutOfBoundsException

  String[] ln = com.split("\\s"); Command = ln[0]; try { Attribute1 = ln[1]; }catch (ArrayIndexOutOfBoundsException exc){ } try { Attribute2 = ln[3]; }catch (ArrayIndexOutOfBoundsException exc){ } try { Attribute3 = ln[4]; }catch (ArrayIndexOutOfBoundsException exc){ } 

Closed due to the fact that off-topic party Nick Volynkin Jul 6 '17 at 3:37 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Nick Volynkin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Add code as you try to do it - Sv__t
  • And what is the error then? Maybe there is only one space in the line? - Tachkin
  • gaps, just enough - Pythonchic
  • 2
    Attach to the question the source line and the output code of the array with the results. - Vladimir Parfenov

2 answers 2

The String class has a special split(String regex) method split(String regex) , which allows you to split a string into parts using the supplied delimiters as a regular expression.

In the case of separation by spaces, everything is simple:

 String[] words = yourStr.split(" "); //words - массив слов без пробелов 
  • regexp is correctly selected: \s allows you to beat tabs in addition to spaces. - Tachkin
  • I'm not special on regulars;) - Vladimir Parfenov
  • changed, added output of finished words (output of variables that are equal to elements in the array) and the last word is null - Pythonchic

I solved the null problem due to the fact that I messed with the numbers of elements in the array and the latter went beyond the limits and it turned out null, thanks to everyone for their help)