Merge remote-tracking branch 'origin/main'

This commit is contained in:
amadoste 2021-11-09 20:42:39 +01:00
commit b190a8d161
1 changed files with 26 additions and 45 deletions

View File

@ -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<String> 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<String> text) {
private void toFormatold(ArrayList<String> 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<String> 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<String> 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.
*/