String input = \"THESE TERMS AND CONDITIONS OF SERVICE (the Terms) ARE A LEGAL AND BINDING AGREEMENT BETWEEN YOU AND NATIONAL GEOGRAPHIC governing your use of this s
My version(The previous were not working)
public static List breakSentenceSmart(String text, int maxWidth) {
StringTokenizer stringTokenizer = new StringTokenizer(text, " ");
List lines = new ArrayList();
StringBuilder currLine = new StringBuilder();
while (stringTokenizer.hasMoreTokens()) {
String word = stringTokenizer.nextToken();
boolean wordPut=false;
while (!wordPut) {
if(currLine.length()+word.length()==maxWidth) { //exactly fits -> dont add the space
currLine.append(word);
wordPut=true;
}
else if(currLine.length()+word.length()<=maxWidth) { //whole word can be put
if(stringTokenizer.hasMoreTokens()) {
currLine.append(word + " ");
}else{
currLine.append(word);
}
wordPut=true;
}else{
if(word.length()>maxWidth) {
int lineLengthLeft = maxWidth - currLine.length();
String firstWordPart = word.substring(0, lineLengthLeft);
currLine.append(firstWordPart);
//lines.add(currLine.toString());
word = word.substring(lineLengthLeft);
//currLine = new StringBuilder();
}
lines.add(currLine.toString());
currLine = new StringBuilder();
}
}
//
}
if(currLine.length()>0) { //add whats left
lines.add(currLine.toString());
}
return lines;
}