import java.util.ArrayList; public class TextOutput { private boolean formatRaw; private int columnWidth; public void print(ArrayList text) { if (formatRaw) { for (int counter = 0; counter < text.size(); counter++) { System.out.println("<" + (counter + 1) + ">: <" + text.get(counter) + ">"); } } else { for (String paragraph : text) { String[] words = paragraph.split(" "); int currentLength = 0; for(String word: words) { if(word.length() > columnWidth){ String[] letters = word.split(""); int letterLenght = letters.length; int lettersPrinted = 0; do{ System.out.println(); currentLength = 0; for(int i=0; i< columnWidth; i++) { if(letterLenght > 0) { System.out.print(letters[lettersPrinted]); letterLenght--; lettersPrinted++; currentLength++; } } } while (letterLenght>columnWidth); } else { if(word.length() >= columnWidth - currentLength){ System.out.println(); currentLength = 0; } System.out.print(word + " "); currentLength += word.length() + 1; } } } } } public void formatRaw() { formatRaw = true; } public void formatFix(int length) { formatRaw = false; columnWidth = length; } }