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; } 
  • one
    “so that under this condition one type word should be printed” - please explain - RusArt
  • I agree, it is not entirely clear. It is assumed if there is a word that is less than the “allowable” length, at the moment it is 8, this length is limited by the size of the image, the condition does not enter and does not work out the entry in the list - Evgeny Leprosy

1 answer 1

If after the loop in resultForRes something is left, then this should also be added to the list ( if after for ). This will help in a situation where there is only one word in the text with a length of less than 7 characters (or several words, the total length of which together with spaces after them is less than 8 characters), and in a situation in which the last word did not fall into res for insufficient length.

 private List<String> divideText(String text) { String[] resSplit = text.split(" "); List<String> res = new LinkedList<>(); String resultForRes = ""; for (int i = 0; i < resSplit.length; i++) { resultForRes += resSplit[i] + " "; if (resultForRes.length() >= 8) { res.add(0, resultForRes); resultForRes = ""; } } if (!resultForRes.isEmpty()) { res.add(0, resultForRes); } return res; } 

It is also worth considering the problem that spaces are added not only between words in the phrase, but also after the last word. In theory, the first word in the phrase should be resultForRes = resSplit[i] , and for subsequent resultForRes += " " + resSplit[i] - resultForRes += " " + resSplit[i] .

  • Thank you very much! I will try to take this into account - Eugene Leprosy