how do I split arrayList String
SCHOOLWORK
BALCONY
INSIST
SALTPETER
BOLTON
KITSCHY
CLIENTELE
I want to split those words to “SCH”, “OOL”, “WO”, “RK”.
Here is my code
import java.io.File;
import java.util.ArrayList;
public class HW2 {
public static ArrayList<String> getTiles(ArrayList<String> input_list_of_strings) {
// create a substring by go through the loop first, then .... (instruction)
Object[] subString = new Object[input_list_of_strings.size()];
for (int i = 0; i < input_list_of_strings.size(); i++) {
subString[i] = input_list_of_strings.get(i);
// test just want to see if subString get all String
// System.out.println(subString[i]);
String delim=" ";
String[] splitstrings = ((String) subString[i]).split(delim);
for (int j = 0; j < splitstrings.length; j++) {
splitstrings[j] +=delim;
System.out.println(splitstrings[j]);
}
}
return input_list_of_strings;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> input_words = new ArrayList<String>();
input_words.add("SCHOOLWORK");
input_words.add("BALCONY");
input_words.add("INSIST");
input_words.add("SALTPETER");
input_words.add("BOLTON");
input_words.add("KITSCHY");
input_words.add("CLIENTELE");
System.out.print(getTiles(input_words));
}
}
Thank you…
I would use an interface to encapsulate how you want to split each string.
Then your ‘getTiles’ method would simply be:
NOTE: This method modifies the original list, it can be easily adapted to simply copy the list if need be. Although, I like it better this way.
Since the exact process for splitting these words is unclear, I am going to guess that you want to keep splitting a word by three characters, unless that would leave only one character, then split by two – else don’t split. So your default ‘StringSplitter’ would be:
Your main method would then be: