I’d like to split a string into an ArrayList.
Example:
String = “Would you like to have responses to your questions”
result with amount 3: Wou -> arraylist, ld -> arraylist, you -> arraylist, …
the amount is a predefined variable.
so far:
public static void analyze(File file) {
ArrayList<String> splittedText = new ArrayList<String>();
StringBuffer buf = new StringBuffer();
if (file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,
Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(isr);
String line = "";
while ((line = reader.readLine()) != null) {
buf.append(line + "\n");
splittedText.add(line + "\n");
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String wholeString = buf.toString();
wholeString.substring(0, 2); //here comes the string from an txt file
}
You are adding each line to your array list, and it doesn’t sound like that is what you want. I think you are looking for something like this: