import java.util.ArrayList; /** * */ public class TextOutput { private boolean formatRaw; private int columnWidth = 10; /** * Method which checks in which way the paragraphs should be displayed. And then calls the corresponding * method to output the text as desired. * * @param text the ArrayList which is then formatted into the desired output. */ public void print(ArrayList text) { if (formatRaw) { printFormated(text); } else { toFormat(text); } } /** * Method to print out the paragraphs in numerical order and with the whole paragraph as its length. * * @param text the ArrayList which is used for the output */ private void printFormated(ArrayList text) { for (int counter = 0; counter < text.size(); counter++) { System.out.println("<" + (counter + 1) + ">: <" + text.get(counter) + ">"); } } private void toFormatold(ArrayList text) { 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 { currentLength = 0; for (int i = 0; i < columnWidth; i++) { if (letterLenght > 0) { System.out.print(letters[lettersPrinted]); letterLenght--; lettersPrinted++; currentLength++; } } if (!(letterLenght == 0)) { System.out.println(); } } while (letterLenght >= columnWidth); } else { if (word.length() >= columnWidth - currentLength) { currentLength = 0; System.out.println(); } System.out.print(word + " "); currentLength += word.length() + 1; } } System.out.println(); //added } } /** * Method to print out the paragraphs with the length taken from columnWidth and adding leftover words to the next * paragraph. * * @param text the ArrayList which is used for the output. */ private void toFormat(ArrayList text) { int currentLength = 0; for (String paragraph : text) { String[] words = paragraph.split(" "); int lastWordNumber = words.length; int currentWordNumber = 0; for(String word : words) { currentWordNumber++; if (word.length()<= columnWidth - currentLength) { System.out.print(word); if (!(lastWordNumber == currentWordNumber)) { System.out.print(" "); currentLength = currentLength + word.length() + 1; } else { System.out.println(); } } else { System.out.println(); System.out.print(word); if(!(currentWordNumber == lastWordNumber)) { System.out.print(" "); currentLength = word.length(); } } } } } /** * Method which sets the Variable formatRaw to true. */ public void formatRaw() { formatRaw = true; } /** * Method to set the formatRaw to false and also set the length of the desired paragraph length in order to limit * the paragraph length when printing the text. * * @param length the maximum length of each paragraph that is allowed to be displayed when printing the text. */ public void formatFix(int length) { formatRaw = false; columnWidth = length; } /** * Method to give out the Error "Invalid String". */ public void errorInvalidString() { System.err.println("Invalid String"); } /** * Method to give out the Error "Invalid Command". */ public void errorInvalidCommand() { System.err.println("Invalid Command"); } }