• Post Reply Bookmark Topic Watch Topic
  • New Topic

wrap text with line break - Urgent

 
Jim Vamvakitis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have for example the code show below, but I want to be able to add line breaks. The reason si to better format paragraphs. Between paragraphs user wants to be able to put 1-4 lines between paragraphs and I need any help or ideas. Thanks again.
<code>
import java.util.*;public class WordFormatter { public static String[] format(String paragraph, int length) { ArrayList formatted = new ArrayList(); StringTokenizer st = new StringTokenizer(paragraph); String next = ""; while (st.hasMoreTokens()) { String line = next; while (line.length() < 30) { if (st.hasMoreTokens()) { next = st.nextToken(); if ((line.length() + next.length()) > 30) { break; } if (!line.equals("")) { line += " "; } line += next; next = ""; } else { break; } } formatted.add(line); } return (String[]) formatted.toArray(new String[0]); } public static void main(String[] args) { String lines = "One feature of the war in Iraq was the speed and immediacy with which many events were reported by the media." + "Some of these turned out to be not quite what they seemed, others are still surrounded by confusion." + "Was this the fog of war, effects-based warfare, propaganda, or error?"; String[] formatted = WordFormatter.format(lines, 30); for (int i = 0; i < formatted.length; i++) { System.out.println(formatted[i]); } }}
</code>
 
bob leduc
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<code>
import java.util.*;
public class WordFormatter {
public static String[] format(String paragraph, int length) {
ArrayList formatted = new ArrayList();
StringTokenizer st = new StringTokenizer(paragraph); String next = "";
while (
st.hasMoreTokens()) {
String line = next;
while (line.length() < 30) {
if (st.hasMoreTokens()) {
next = st.nextToken();
if ((line.length() + next.length()) > 30) {
break;
}
if (!line.equals("")) {
line += " ";
}
line += next; next = "";
} else {
break;
}
}
formatted.add(line);
}
return (String[]) formatted.toArray(new String[0]);
}

public static void main(String[] args) {
String lines = "One feature of the war in Iraq was the speed and immediacy with which any events were reported by the media." + "Some of these turned out to be not quite what they seemed, others are still surrounded by confusion." + "Was this the fog of war, effects-based warfare, propaganda, or error?";
String[] formatted = WordFormatter.format(lines, 30);
for (int i = 0; i < formatted.length; i++) {
System.out.println(formatted[i]);
}
}
}
</code>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!