diff --git a/src/TextOutput.java b/src/TextOutput.java index b409190..91fde26 100644 --- a/src/TextOutput.java +++ b/src/TextOutput.java @@ -1,5 +1,4 @@ import java.util.ArrayList; -import java.util.Iterator; public class TextOutput { @@ -9,13 +8,13 @@ public class TextOutput { /** * Method which checks in which way the paragraphs should be displayed. And then * - * @param text the ArrayList which is then formated into the desired output. + * @param text the ArrayList which is then formatted into the desired output. */ public void print(ArrayList text) { if (formatRaw) { printFormated(text); } else { - printUnformatedV2(text); + toFormat(text); } } @@ -37,7 +36,7 @@ public class TextOutput { * * @param text the ArrayList which is used for the output. */ - private void printUnformated(ArrayList text) { + private void toFormatold(ArrayList text) { for (String paragraph : text) { String[] words = paragraph.split(" "); int currentLength = 0; @@ -74,54 +73,36 @@ public class TextOutput { System.out.println(); //added } } - private void printUnformatedV2(ArrayList text) { - for(String paragraph : text) { - if (paragraph.length() <= columnWidth) { - System.out.println(paragraph); - } - else { - String[] word = paragraph.split(" "); - int currentLineLength = 0; - for(String eachword : word) { - if (eachword.length() > columnWidth) { - String remainingText = eachword; - do { - if (remainingText.length() < columnWidth) { - System.out.println(remainingText); - break; - } - System.out.println(remainingText.substring(0, columnWidth)); - remainingText = remainingText.substring((columnWidth)); - - } while (!(remainingText.length() == 0)); + 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 { - if (currentLineLength == 0) { - System.out.println(); + System.out.println(); } - if (columnWidth > eachword.length()) { - System.out.print(eachword); - if (!(currentLineLength == columnWidth)) { - System.out.print(" "); - } - currentLineLength = currentLineLength + eachword.length(); - } - else { - System.out.println(eachword); - currentLineLength = eachword.length(); - if (!(currentLineLength == columnWidth)) { - System.out.print(" "); - } - } - - - } - } - + 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. */