Word Wrap not functioning correctly?
I made a "utility" class for an assignment that I have to do. It provides methods and what not to produce simple reports. One method I have is printParagraph.
It supports word wrap, or atleast, I have tried to.
The problem is, I have a if statement that tells the compiler, if the amount of string left in the paragraph is less then my the startingPoint of the substring +the textable width of the page (pageColumns), then just print a substring from the startpoint to the end of the paragraph string (aka , stringLength).
However, it seems to be jumping the gun. stringLength - startPoint must be less then or equal to -1 , in order to for it to print the startPoint to stringLength substring. It seems to execute this when it even isn't (after debuging the number comes to 107 or so in this example.)
Here is my code.
Code:
public class ReportUtility {
private final int pageColumns;
private final int pageLines;
private final String authorName;
private int lineCounter;
private int pageNumber;
private String margin;
public ReportUtility(int pageCol, int pageLin, String Author){
pageColumns = pageCol;
pageLines = pageLin;
authorName = Author;
pageNumber = 1;
lineCounter = 1;
margin = " ";
}
public void reportChecker(){
if (pageLines == lineCounter){
pageNumber++;
lineCounter = 1;
}
if (pageNumber > 1){
System.out.printf("%s%-s Page:%-d%s", margin, authorName, pageNumber, margin);
System.out.println();
System.out.println();
lineCounter += 3;
}
}
public void printTitle (String title){
int spacing = (pageColumns - title.length())/2;
String spaces = " ";
for(int i = 0;i < spacing;i++){
spaces += " ";
}
System.out.printf("%s%s%s%n", spaces, title, spaces);
System.out.println();
System.out.println();
lineCounter += 3;
reportChecker();
}
public void printParagraph (String paragraph){
reportChecker();
int stringLength = paragraph.length();
if (stringLength <= pageColumns){
System.out.println(paragraph);
lineCounter++;
reportChecker();
}
if (stringLength >= pageColumns){
int startPoint = 0;
int startIndex = pageColumns - margin.length();
int lastSpace = paragraph.lastIndexOf(" ",startIndex);
int lastPeriod = paragraph.lastIndexOf(".",startIndex);
if (lastSpace < lastPeriod){
startPoint = lastPeriod;
}
if (lastPeriod < lastSpace){
startPoint = lastSpace;
}
System.out.println(margin + paragraph.substring(0, startPoint));
lineCounter++;
reportChecker();
while(startPoint < stringLength){
lastSpace = paragraph.lastIndexOf(" ",startPoint + pageColumns );
lastPeriod = paragraph.lastIndexOf(".",startPoint + pageColumns);
if (lastSpace < lastPeriod){
System.out.println(paragraph.substring(startPoint, lastPeriod));
startPoint = lastPeriod;
}
if (lastPeriod < lastSpace){
System.out.println(paragraph.substring(startPoint, lastSpace));
startPoint = lastSpace;
}
lineCounter++;
reportChecker();
[B]if(stringLength - startPoint <= -1);
System.out.println(paragraph.substring(startPoint, stringLength));
break;
}[/B]
if(lineCounter != pageLines){
System.out.println();
lineCounter++;
reportChecker();
}
}
}
And here is the "TestReport" class that puts the parameters of the page/report.
Code:
public class TestReport {
public static void main(String [ ] args){
ReportUtility report = new ReportUtility(20, 15, "Author Name" );
report.printTitle("The Man in the Sand");
report.printParagraph("The man walked to the beach. He had tons of fun, but he got sand in his shoes. The man was fairly mad. He threw his shoes in the ocean.");
}
}
And here is the output :
Code:
The Man in the Sand
The man walked
to the beach. He
had tons of fun, but he got sand in his shoes. The man was fairly mad. He threw his shoes in the ocean.
Any help guys?