Results 1 to 3 of 3
  1. #1
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default 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.

    Java 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.

    Java 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 :
    Java 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you either used a debugger to walk through your program and see what it's doing, or else use the "poor-man's debugger" of println statements outputting program state either to standard output or a file?

  3. #3
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    I've used the "poor-man's debugger" . I have eclipse, but im not as experienced with the debugger.

    The number I got for (stringLength - startPoint) is 104.

    its obviously above 0. but the print statement that gave me that is executed ONLY when the if statement that gives the condition of (stringLength - startPoint > 0;)

Similar Threads

  1. Applet not functioning
    By Shadaw in forum Java Applets
    Replies: 2
    Last Post: 12-18-2008, 05:15 AM
  2. Eclipse import line wrap
    By bogomips in forum Eclipse
    Replies: 0
    Last Post: 11-26-2008, 04:16 PM
  3. How to create tool bar (wrap on resize)
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •