Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi...I'm writing a wordwrap Minimum raggedness...which has been explained in :
http://en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness[^]
I wrote the program and it calculate the cost of each word if it's not negative (I mean if there's place for word in the line)...the code is like that :
Java
package wordwrap;
import java.io.*;
import java.util.*;
import java.math.*;
/**
 *
 * @author ahangari
 */
public class WordWrap {

       String strtmp=null;
       String delimiter =" ";
       String[] words=new String[100];
       int linewidth=0;
       int i,j,k;
       int stage;
       int sum=0;
       int space=0;
       int totalspace=0;
       int min=0;
       int cost=-1;//baraye inke shartemun ine ke cost>=0 bashe
       int[][] fcost=new int[100][100];//an array for storing final cost
       int[] check=new int[100];
       int p=0;
       int finalsum1=0;
       int[] finalsum2=new int[1000];
       //*********************************************
       public void Init(){
           for(i=0;i<fcost.length;i++){
              for(j=0;j<fcost.length;j++){
              fcost[i][j]=-1;
              }
           }
       }
       public void inputfile() throws FileNotFoundException {
      try{
        Scanner wr=new Scanner(new FileInputStream("wordwrap.txt"));

       strtmp = wr.nextLine();
       words = strtmp.split(delimiter);

   wr.close();

    }

catch (Exception e){
        System.out.println(e+"read exception");

}
    }
       //*********************************************
       public void entries(){
           Scanner scan=new Scanner(System.in);
           System.out.println("Please Enter the line width :");
           linewidth=scan.nextInt();
           //for(i=0;i<words.length;i++){
           //if(linewidth<words[i].length())
             //  System.out.printf("The text can't be wraped into %d column.",linewidth);
           //break;
          // }
       }
       //*********************************************
       public void cost(){
       
        for(i=0 ;i<words.length; i++){
           for(j=i;j<words.length;j++){
               space=j-i;
               sum=words[i].length();
               if(i==j)
                  {
                 totalspace=linewidth;
                 cost=(totalspace-sum);
               }
 else{
               totalspace=linewidth-space;//chon fazaye beyne kalame ha 1space
               cost=0;
                if(totalspace>0){                     //ast pas miad tedade space ha ro az kole linewidth kam mikone
               sum+=words[j].length();
               cost=(totalspace-sum);
               }
                      }
               if(cost>=0)
                              {
                   fcost[i][j]=(cost)*(cost);
                 System.out.printf("The cost of word %d to %d is :%d \n ",i,j,fcost[i][j]);
               }
                
                cost=0;
       }
           sum=0;// baraye inke maghadire ghabli ruye stage baadi asar nazare
       }
}

but I don't know how to calculate the minimum raggedness and print the form of minimum raggedness...in this code finally I have the costs in "fcost[i][j]" array...for example fcost[0][1] can be "0" and this means that if we put words[0] and words[1] in line[0] the cost would be "0"...can anyone help me continiuing this code?
Posted
Comments
Sergey Alexandrovich Kryukov 31-Jan-12 12:40pm    
This is not a question. Not clear.
--SA
TorstenH. 1-Feb-12 1:48am    
sounds fine to me if the line is filled out completely - or are you concerned to loose a space between words on the linefeed?

minimum raggedness - what do you mean by that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900