From 300d41c53db6da3d561a1ae008ac555211898c55 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Tue, 9 Nov 2021 16:56:20 +0100 Subject: [PATCH] added a new prototype for a better toFormat method --- src/TextOutput.java | 62 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/src/TextOutput.java b/src/TextOutput.java index b409190..9423199 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); + toFormat4(text); } } @@ -30,6 +29,25 @@ public class TextOutput { } } + private void toFormat2(ArrayList text) { + for (String para : text) { + String[] words = para.split(" "); + String output = ""; + for (String word : words) { + if ((output.length() + word.length()) < columnWidth) { + output += "word "; + } else { + if (word.length() <= columnWidth) { + System.out.println(output); + output = ""; + } else { + System.out.println(word.substring(0, columnWidth) );//+ "-"); + output = word.substring(columnWidth); + } + } + } + } + } /** * Method to print out the paragraphs with the length taken from columnWidth and adding leftover words to the next @@ -37,7 +55,7 @@ public class TextOutput { * * @param text the ArrayList which is used for the output. */ - private void printUnformated(ArrayList text) { + private void toFormat(ArrayList text) { for (String paragraph : text) { String[] words = paragraph.split(" "); int currentLength = 0; @@ -74,7 +92,41 @@ public class TextOutput { System.out.println(); //added } } - private void printUnformatedV2(ArrayList text) { + private void toFormat4(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.println(); + } + else { + System.out.print(" "); + currentLength = word.length(); + } + } + } + } + } + + private void toFormat3(ArrayList text) { for(String paragraph : text) { if (paragraph.length() <= columnWidth) { System.out.println(paragraph);