From 300d41c53db6da3d561a1ae008ac555211898c55 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Tue, 9 Nov 2021 16:56:20 +0100 Subject: [PATCH 1/2] 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); From e96f492386fb920f023902467fd9c0170508b1b9 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Tue, 9 Nov 2021 17:06:46 +0100 Subject: [PATCH 2/2] removed old toFormat Method --- src/TextOutput.java | 79 +++------------------------------------------ 1 file changed, 4 insertions(+), 75 deletions(-) diff --git a/src/TextOutput.java b/src/TextOutput.java index 9423199..91fde26 100644 --- a/src/TextOutput.java +++ b/src/TextOutput.java @@ -14,7 +14,7 @@ public class TextOutput { if (formatRaw) { printFormated(text); } else { - toFormat4(text); + toFormat(text); } } @@ -29,25 +29,6 @@ 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 @@ -55,7 +36,7 @@ public class TextOutput { * * @param text the ArrayList which is used for the output. */ - private void toFormat(ArrayList text) { + private void toFormatold(ArrayList text) { for (String paragraph : text) { String[] words = paragraph.split(" "); int currentLength = 0; @@ -92,7 +73,7 @@ public class TextOutput { System.out.println(); //added } } - private void toFormat4(ArrayList text) { + private void toFormat(ArrayList text) { int currentLength = 0; for (String paragraph : text) { String[] words = paragraph.split(" "); @@ -109,15 +90,11 @@ public class TextOutput { else { System.out.println(); } - } else { System.out.println(); System.out.print(word); - if(currentWordNumber == lastwordNumber) { - //System.out.println(); - } - else { + if(!(currentWordNumber == lastwordNumber)) { System.out.print(" "); currentLength = word.length(); } @@ -126,54 +103,6 @@ public class TextOutput { } } - private void toFormat3(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)); - } - else { - if (currentLineLength == 0) { - 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(" "); - } - } - - - } - - } - - } - } - } /** * Method which sets the Variable formatRaw to true. */