2 years ago
#253826
MK123
Word wrap and full justify text so that each line has the same length
I am trying to create a class HW2 using the prettyPrint method. I am trying to do a word wrap and full justify text so that each line has the same length (using System.out.println). I am trying to get something to look like this:
HW2.prettyPrint("This homework is demonstrating how word processors make the text in a document line up so neatly.", 25)
This     homework      is
demonstrating  how   word
processors make the  text
in a document line up  so
neatly.
And this is my code that is not working - any ideas on how to fix?
  public static void prettyPrint(String s, int x) {
    int numberWords = countWords(s);
    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < s.length(); i++){
      if(i <= x){
        padString(s, x);
        builder.append(s.charAt(i));
      }
      if(i == x){
        System.out.println();
      }
    }
    System.out.println();
    // use CountWords, separate words into an array, then concatenate every line and use padString to output the correct length
    // print one line first, then move on to another
  }
}
java
word-wrap
justify
0 Answers
Your Answer