2021-11-05 12:03:27 +01:00
|
|
|
import java.util.ArrayList;
|
2021-11-09 10:54:26 +01:00
|
|
|
|
2021-11-05 12:03:27 +01:00
|
|
|
|
|
|
|
public class TextOutput {
|
|
|
|
private boolean formatRaw;
|
2021-11-05 15:08:25 +01:00
|
|
|
private int columnWidth = 10;
|
2021-11-05 12:03:27 +01:00
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
/**
|
|
|
|
* Method which checks in which way the paragraphs should be displayed. And then
|
|
|
|
*
|
2021-11-09 16:56:20 +01:00
|
|
|
* @param text the ArrayList which is then formatted into the desired output.
|
2021-11-09 10:54:26 +01:00
|
|
|
*/
|
2021-11-05 12:03:27 +01:00
|
|
|
public void print(ArrayList<String> text) {
|
2021-11-05 15:08:25 +01:00
|
|
|
if (formatRaw) {
|
|
|
|
printFormated(text);
|
2021-11-08 20:45:44 +01:00
|
|
|
} else {
|
2021-11-09 16:56:20 +01:00
|
|
|
toFormat4(text);
|
2021-11-05 15:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-11-05 15:08:25 +01:00
|
|
|
private void printFormated(ArrayList<String> text) {
|
2021-11-05 12:03:27 +01:00
|
|
|
for (int counter = 0; counter < text.size(); counter++) {
|
|
|
|
System.out.println("<" + (counter + 1) + ">: <" + text.get(counter) + ">");
|
|
|
|
}
|
2021-11-05 15:08:25 +01:00
|
|
|
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
2021-11-09 16:56:20 +01:00
|
|
|
private void toFormat2(ArrayList<String> 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 20:45:44 +01:00
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-11-09 16:56:20 +01:00
|
|
|
private void toFormat(ArrayList<String> text) {
|
2021-11-05 12:03:27 +01:00
|
|
|
for (String paragraph : text) {
|
|
|
|
String[] words = paragraph.split(" ");
|
|
|
|
int currentLength = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
for (String word : words) {
|
|
|
|
if (word.length() > columnWidth) {
|
2021-11-05 12:03:27 +01:00
|
|
|
String[] letters = word.split("");
|
|
|
|
int letterLenght = letters.length;
|
|
|
|
int lettersPrinted = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
do {
|
2021-11-05 12:03:27 +01:00
|
|
|
currentLength = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
for (int i = 0; i < columnWidth; i++) {
|
|
|
|
if (letterLenght > 0) {
|
2021-11-05 12:03:27 +01:00
|
|
|
System.out.print(letters[lettersPrinted]);
|
|
|
|
letterLenght--;
|
|
|
|
lettersPrinted++;
|
|
|
|
currentLength++;
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 20:45:44 +01:00
|
|
|
if (!(letterLenght == 0)) {
|
2021-11-05 16:16:39 +01:00
|
|
|
System.out.println();
|
|
|
|
}
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
2021-11-05 16:16:39 +01:00
|
|
|
while (letterLenght >= columnWidth);
|
2021-11-05 12:03:27 +01:00
|
|
|
|
2021-11-08 20:45:44 +01:00
|
|
|
} else {
|
2021-11-05 15:08:25 +01:00
|
|
|
if (word.length() >= columnWidth - currentLength) {
|
2021-11-05 12:03:27 +01:00
|
|
|
currentLength = 0;
|
2021-11-05 15:08:25 +01:00
|
|
|
System.out.println();
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
|
|
|
System.out.print(word + " ");
|
|
|
|
currentLength += word.length() + 1;
|
2021-11-05 16:29:29 +01:00
|
|
|
}
|
2021-11-05 16:16:39 +01:00
|
|
|
}
|
|
|
|
System.out.println(); //added
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 16:56:20 +01:00
|
|
|
private void toFormat4(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 {
|
|
|
|
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<String> text) {
|
2021-11-09 10:54:26 +01:00
|
|
|
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));
|
2021-11-05 12:03:27 +01:00
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
} 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.
|
|
|
|
*/
|
2021-11-05 12:03:27 +01:00
|
|
|
public void formatRaw() {
|
|
|
|
formatRaw = true;
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-11-05 12:03:27 +01:00
|
|
|
public void formatFix(int length) {
|
2021-11-08 20:45:44 +01:00
|
|
|
formatRaw = false;
|
|
|
|
columnWidth = length;
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|
2021-11-08 20:45:44 +01:00
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
/**
|
|
|
|
* Method to give out the Error "Invalid String".
|
|
|
|
*/
|
2021-11-05 15:08:25 +01:00
|
|
|
public void errorInvalidString() {
|
|
|
|
System.err.println("Invalid String");
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
/**
|
|
|
|
* Method to give out the Error "Invalid Command".
|
|
|
|
*/
|
2021-11-05 15:08:25 +01:00
|
|
|
public void errorInvalidCommand() {
|
|
|
|
System.err.println("Invalid Command");
|
|
|
|
}
|
2021-11-05 12:03:27 +01:00
|
|
|
}
|