From the code, a list of phrases formed by length is output, which are further printed on the image.
I can’t think of a condition so that one word can be printed under this condition. Maybe you need some extra if ?
private List<String> divideText(String text) { String[] resSplit = text.split(" "); List<String> res = new LinkedList<String>(); String resultForRes = ""; int countLength = 0; for (int i = 0; i < resSplit.length; i++) { resultForRes += resSplit[i] + " "; countLength = resultForRes.length(); if (countLength >= 8) { res.add(0, resultForRes); resultForRes = ""; countLength = 0; } } return res; }