Hi.
I am passing different kind of text file and calling same function.. Can any one know how to avoid these repeated code.

public static void main(String[] args) throws IOException,NullPointerException
{
              
		MultipleLinearRegrssionModel fex = new MultipleLinearRegrssionModel();
		//Calls Read From File 
                //First ocument
                docsum=1;
		fex.readFromFile("f:/project/document/training/1.txt");
                //Calls compare training corpus and manual summary
                doccom=1;
                fex.strCompare();
		//Calls Stop Words Removing             
		fex.stoprem();
		//Term Frequency Calculating
		fex.termFreq();
		//Calls Feature F1
		fex.fOne();
		//Calls Feature F2
		fex.fTwo();
		//Calls Feature F2
		fex.fThree();
		//Calls Feature F4
		fex.fFour();
		//Calls Feature F5
		fex.fFive();
		//Calls Feature F6
		fex.fSix();
		//Calls Feature F7
		fex.fSeven();
		//Calls Feature F8
		fex.fEight();
		//Calls Feature F9
		fex.fNine();
		//calls Feature F10
		fex.fTen();
                //sum of indepentant variable
                fex.indepentant();
                //weight calculation
                //initialize
                fex.intiaddweight();
               // System.out.println("addtoweight");                
                fex.addtotweight();
                //Calls Write To File
                fex.writeToFile("f:/project/out.txt");
                //calls empty
                fex.empty();
                         
                //Second document  
                docsum=2;
                fex.readFromFile("f:/project/document/training/2.txt");
                //compare
                doccom=2;
                fex.strCompare();
		//Calls Stop Words Removing
                fex.stoprem();
		//Term Frequency Calculating
		fex.termFreq();
		//Calls Feature F1
		fex.fOne();
		//Calls Feature F2                
		fex.fTwo();
		//Calls Feature F2
		fex.fThree();
		//Calls Feature F4
		fex.fFour();
		//Calls Feature F5
		fex.fFive();
		//Calls Feature F6
		fex.fSix();
		//Calls Feature F7
		fex.fSeven();
		//Calls Feature F8
		fex.fEight();
		//Calls Feature F9
                fex.fNine();
		//calls Feature F10
		fex.fTen();
                //indepentant();
                fex.indepentant();
                //addtoweight
                fex.addtotweight();
                //Calls Write To File
                fex.writeToFile("f:/project/out1.txt");
             
                //calls empty
                fex.empty();
                
                //Third document
                
               
                docsum=3;
                fex.readFromFile("f:/project/document/training/3.txt");
                //compare
                doccom=3;
                fex.strCompare();
		//Calls Stop Words Removing
                fex.stoprem();
		//Term Frequency Calculating
		fex.termFreq();
		//Calls Feature F1
		fex.fOne();
		//Calls Feature F2                
		fex.fTwo();
		//Calls Feature F2
		fex.fThree();
		//Calls Feature F4
		fex.fFour();
		//Calls Feature F5
		fex.fFive();
		//Calls Feature F6
		fex.fSix();
		//Calls Feature F7
		fex.fSeven();
		//Calls Feature F8
		fex.fEight();
		//Calls Feature F9
                fex.fNine();
		//calls Feature F10
		fex.fTen();
                //indepentant();
                fex.indepentant();
                //addtoweight
                fex.addtotweight();
                //Calls Write To File
                fex.writeToFile("f:/project/out1.txt");
                //avgweight
                fex.avgweight();
                //calls empty
                fex.empty();
                
                
               //Fourth document
          
                fex.readFromFile1("f:/project/document/training/3.txt");
                //Calls Stop Words Removing
                fex.stoprem();
		//Term Frequency Calculating
		fex.termFreq();
		//Calls Feature F1
		fex.fOne();
		//Calls Feature F2                
		fex.fTwo();
		//Calls Feature F2
		fex.fThree();
		//Calls Feature F4
		fex.fFour();
		//Calls Feature F5
		fex.fFive();
		//Calls Feature F6
		fex.fSix();
		//Calls Feature F7
		fex.fSeven();
		//Calls Feature F8
		fex.fEight();
		//Calls Feature F9
                fex.fNine();
		//calls Feature F10
		fex.fTen();
                //calling Testing
                fex.testing();       
                fex.writeToFile1("f:/project/out2.txt");    
               
                fex.manVsMachine();
                fex.testFinalwrite("f:/project/out2final.txt");
	
	}
}

So the above different document calling same function. I am calling one document and for that document some set of function is called.
Next time i am calling different document for the same function.

So my code become very lengthy... can any one suggest to avoid the repeated code. How to call the same function for using different kind document by writing the code only once....

Recommended Answers

All 2 Replies

Write a method.

HI ....

Below readfromfile is there.... can you able to tell in what way i can avoid the repeated code....

public void readFromFile(String fileName)throws IOException
{    
      	String str = new String();    	
    	String str1 = new String();    	
        String str2 =new String();
	BufferedReader br=new BufferedReader(new FileReader(fileName));    	
	String sstr = new String();        
	String sstr1 = new String();
        String sstr2 = new String();        
	String sstr22 = new String();
        String sstr3 = new String();        
	String sstr33 = new String();
	BufferedReader sbr=new BufferedReader(new FileReader("f:/project/document/training/1sum.txt"));
        BufferedReader sbr1=new BufferedReader(new FileReader("f:/project/document/training/2sum.txt"));
        BufferedReader sbr2=new BufferedReader(new FileReader("f:/project/document/training/3sum.txt"));
   	while((str= br.readLine()) != null)
		{
    		//Analysis
    		//Separating Sentences
    		//Converting all Upper Case to Lower Case
    			StringTokenizer st = new StringTokenizer(str, ".");
    			int i = 0;
    			while(st.hasMoreTokens())
                        {
    				i = i + 1;                                
    				//str1 = st.nextToken() ;
                                str1=st.nextToken()+".";
                                //  str2=str1;
                               // original content with dot
                                   //words2.addElement(str2);
                               // str1 = str1.toLowerCase();
                               //Words1 vector is used for to store all the training document sentence
    				words1.addElement(str1);
         			//str1 = str1.toLowerCase();
    				str1 = i + str1;
                              // System.out.println("words 1 : "+words1);
    				words.addElement(str1);
                                  //System.out.println("words : "+words);                                
    			}    			
    	}        
        //second document   
	//Same for Summary Documents
	switch(docsum)
        {
            case 1:
            while((sstr= sbr.readLine()) != null)
	{
		//Analysis
		//Separating Sentences
		//Converting all Upper Case to Lower Case
			StringTokenizer st = new StringTokenizer(sstr, ".");
			int i = 0;
			while(st.hasMoreTokens())
                        {
				i = i + 1;
	                       	sstr1 = st.nextToken() + ".";
                                //sstr1 = sstr1.toLowerCase();
				//Summary vector is used for to store all the manual summary sentence
                        	summary.addElement(sstr1);
	
			}
			
	}
        break;
            case 2:
                while((sstr2= sbr1.readLine()) != null)
	{
		//Analysis
		//Separating Sentences
		//Converting all Upper Case to Lower Case
			StringTokenizer st = new StringTokenizer(sstr2, ".");
			int i = 0;
			while(st.hasMoreTokens())
                        {
				i = i + 1;
	                       	sstr22 = st.nextToken() + ".";
                                //sstr1 = sstr1.toLowerCase();
				//Summary vector is used for to store all the manual summary sentence
                        	summary1.addElement(sstr22);
				
			}
			
	}
                break;
            case 3:
                while((sstr3= sbr2.readLine()) != null)
	{
		//Analysis
		//Separating Sentences
		//Converting all Upper Case to Lower Case
			StringTokenizer st = new StringTokenizer(sstr3, ".");
			int i = 0;
			while(st.hasMoreTokens())
                        {
				i = i + 1;
	                       	sstr33 = st.nextToken() + ".";
                                //sstr1 = sstr1.toLowerCase();
				//Summary vector is used for to store all the manual summary sentence
                        	summary2.addElement(sstr33);
				
			}
			
	}
        }   	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.