Wednesday 15 April 2015

java - An array out of bounds error -



java - An array out of bounds error -

i want write code splits string separate substrings each 3 characters , assign them string array illustration string "abcdefg" can spit "abc", "bcd", "cde", "efg" , these assigned string array. have next code getting error:

string[] words = new string[] {}; string sequence = "abcdefg"; int i; for(i = 0; <= sequence.length()-3; i++) { words[i] = sequence.substring(i, 3+i); system.out.println(words[i]); }

use next code instead:

string sequence="abcdefg"; string[] words=new string[sequence.length()-2]; int i; for(i=0;i<=(sequence.length()-3);i++){ words[i]=sequence.substring(i,3+i); system.out.println(words[i]); }

or utilize arraylist of strings

java

No comments:

Post a Comment