patternjavaMinor
Word Wrapping and Boxing
Viewed 0 times
andboxingwordwrapping
Problem
This was an experiment to take any text and wrap it to a given number of columns. It also wraps
the text in just one box, lets you box multiple blocks of text, and also lets you limit the number
of boxes per line. The main paragraph tested was one that I found on Yahoo that I thought was
really fun to use as a test. Would you be willing to look at my code for clarity and the algorithm
for efficiency?
WordWrap.java
```
package wordwrap;
public class WordWrap {
private static String wordWrap(String text, int width, String delim) {
String out = "";
String[] words;
int currentWidth = 0;
//Parse out tabs and new lines
text = text.replaceAll("[\t\n]", " ");
words = text.split(delim);
//Rewrap to new width
for (String word : words) {
if (word.length() >= width) {
//If it's not the first word, put it on a new line
if (!out.isEmpty()) {
out += "\n";
}
out += word + " ";
currentWidth = word.length();
}
else if ((currentWidth + word.length()) maxBoxLines) {
maxBoxLines = boxLines;
}
}
lines = new String[boxes.length][maxBoxLines];
for (int b = 0; b = lines[b].length) {
currentLine += String.format("%" + width + "s ", "");
}
else {
currentLine += lines[b][l] + " ";
}
}
out += currentLine.substring(0, currentLine.length() - 1) + "\n";
}
}
return out;
}
public static String drawBoxes(String[] messages, int width, int boxesWide) {
String out = "";
if (messages.length messages.length - currentBox) {
boxesWide = messages.length - currentBox;
}
currentMessages =
the text in just one box, lets you box multiple blocks of text, and also lets you limit the number
of boxes per line. The main paragraph tested was one that I found on Yahoo that I thought was
really fun to use as a test. Would you be willing to look at my code for clarity and the algorithm
for efficiency?
WordWrap.java
```
package wordwrap;
public class WordWrap {
private static String wordWrap(String text, int width, String delim) {
String out = "";
String[] words;
int currentWidth = 0;
//Parse out tabs and new lines
text = text.replaceAll("[\t\n]", " ");
words = text.split(delim);
//Rewrap to new width
for (String word : words) {
if (word.length() >= width) {
//If it's not the first word, put it on a new line
if (!out.isEmpty()) {
out += "\n";
}
out += word + " ";
currentWidth = word.length();
}
else if ((currentWidth + word.length()) maxBoxLines) {
maxBoxLines = boxLines;
}
}
lines = new String[boxes.length][maxBoxLines];
for (int b = 0; b = lines[b].length) {
currentLine += String.format("%" + width + "s ", "");
}
else {
currentLine += lines[b][l] + " ";
}
}
out += currentLine.substring(0, currentLine.length() - 1) + "\n";
}
}
return out;
}
public static String drawBoxes(String[] messages, int width, int boxesWide) {
String out = "";
if (messages.length messages.length - currentBox) {
boxesWide = messages.length - currentBox;
}
currentMessages =
Solution
String concatenation
String concatenation using
Preparation before splitting
The replacement before splitting could be improved here:
Multiple consecutive whitespace characters will result in multiple consecutive space characters. And since the default delimiter is space, the words array may contain empty elements. I recommend to adjust the replacement pattern for better results:
Declare variables right before you need them
In many places of there code you declare variables at the top of a function, even if they won't be used by all execution branches. This is not recommended. It's best to declare variables right before you need them. This is to minimize the live time, which is a window of vulnerability when the variable can be misused, leading to bugs.
Unit testing
Instead of printing formatted text to standard output, this kind of functionality really begs for unit testing, where you assert the expected outputs, which automated the verification step for you, so that you don't have to re-read and verify with your eyes.
String concatenation using
+= is inefficient. Use a StringBuilder instead.Preparation before splitting
The replacement before splitting could be improved here:
//Parse out tabs and new lines
text = text.replaceAll("[\t\n]", " ");
words = text.split(delim);Multiple consecutive whitespace characters will result in multiple consecutive space characters. And since the default delimiter is space, the words array may contain empty elements. I recommend to adjust the replacement pattern for better results:
text = text.replaceAll("\\s+", " ");Declare variables right before you need them
In many places of there code you declare variables at the top of a function, even if they won't be used by all execution branches. This is not recommended. It's best to declare variables right before you need them. This is to minimize the live time, which is a window of vulnerability when the variable can be misused, leading to bugs.
Unit testing
Instead of printing formatted text to standard output, this kind of functionality really begs for unit testing, where you assert the expected outputs, which automated the verification step for you, so that you don't have to re-read and verify with your eyes.
Code Snippets
//Parse out tabs and new lines
text = text.replaceAll("[\t\n]", " ");
words = text.split(delim);text = text.replaceAll("\\s+", " ");Context
StackExchange Code Review Q#123707, answer score: 2
Revisions (0)
No revisions yet.