word Wrap help, words spilling over word limit!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • berky
    New Member
    • Jan 2011
    • 16

    word Wrap help, words spilling over word limit!

    hello my wonderful internet users!

    I have been assigned a simple word wrap program. I think I've gotten so wrapped up in the method I'm trying to implement, but it's just not coming together.


    the logic:
    so I have a variable that counts the length of the line allowed "read in a width"

    -I have a variable that counts the length of said list.

    -I have something to check to see if the word being messed with is the first in the line or not.


    -I check to see if first is true or false
    -set the length of the character countering variable

    if the CC is less the the width I Want, but not a first word then print a space and the word

    -if it is the first word and less then the width then just print the word

    and if it exceeds then print a new line and then the word.

    but I am getting spill over :(!! its going a little beyond the width each time.

    Code:
    for (String word: words) {
    
    		if(cc==0 ||cc ==word.length()){
    			first=true;}
    	        if(cc !=0 && cc !=word.length()){
    			first=false; } 
    	     
    
    //if(line.trim().length()==0){out.printf("ANYBONDY HEA ME!!!");}
    
    		cc=cc+word.length();
    		
    		if(cc>width){
    		//out.printf("%d",cc);
    		out.printf("%n");
    		 cc=word.length();
    			first=true;
    				}
    
    //if(cc==0 ||cc ==word.length()){
    //			first=true;}
    
    		if(first==true){
    		out.printf("%s",word);}
    
    		
    		//if(cc>= width){out.printf("%n");}
    		if(cc  <=width&&first==false){
    				out.printf(" ");
    				out.printf ("%s", word);
    
    			cc=cc+1;
    //out.printf("%d",cc);
    }
    /*
    if(cc==0 ||cc ==word.length()){
    			first=true;}
    	         if(cc !=0 && cc !=word.length()){
    			first=false; } 
    	     if(first==true){cc=cc+word.length();
    out.printf("%s",word);}
    else{
    cc=cc+1;
    if(cc+word.length()<width){
    out.printf(" ");
    out.printf("%s",word);}
    else{
    out.printf("%n");
    cc=0;
    out.printf("%s",word);
    cc=cc+word.length();
    }
    }
    if(first){out.printf("%d",cc);}
    */
     		
         }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    I have trouble understanding what are you trying to achieve.
    Can you elaborate the problem bit for me?
    Also post whole code here. So that i can get an idea.

    Regards
    Dheeraj Joshi

    Comment

    Working...