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 17:06:46 +01:00
|
|
|
toFormat(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-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 17:06:46 +01:00
|
|
|
private void toFormatold(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 17:06:46 +01:00
|
|
|
private void toFormat(ArrayList<String> text) {
|
2021-11-09 16:56:20 +01:00
|
|
|
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);
|
2021-11-09 17:06:46 +01:00
|
|
|
if(!(currentWordNumber == lastwordNumber)) {
|
2021-11-09 16:56:20 +01:00
|
|
|
System.out.print(" ");
|
|
|
|
currentLength = word.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 10:54:26 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|