added a new prototype for a better toFormat method
This commit is contained in:
		
							parent
							
								
									4c304e06e3
								
							
						
					
					
						commit
						300d41c53d
					
				| 
						 | 
				
			
			@ -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);
 | 
			
		||||
            toFormat4(text);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -30,6 +29,25 @@ public class TextOutput {
 | 
			
		|||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    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);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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<String> text) {
 | 
			
		||||
    private void toFormat(ArrayList<String> 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<String> text) {
 | 
			
		||||
    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) {
 | 
			
		||||
        for(String paragraph : text) {
 | 
			
		||||
            if (paragraph.length() <= columnWidth) {
 | 
			
		||||
                System.out.println(paragraph);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue