2 Replies - 305 Views - Last Post: 12 October 2012 - 01:22 PM Rate Topic: -----

#1 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Manipulating words from the file

Posted 05 October 2012 - 12:41 PM

The code prints different reports my question is how to print the text from the file in columns of 5 words each per line. The method that supposed to do that, is public void printTokens() but not sure if the loop is properly set up.     import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;


public class WordsManipulationClient {

	
	public static void main(String[] args)
	{
		WordFrequencyService service1 = new WordFrequencyService();
		service1.readPrintText();
		System.out.println();
		service1.storeTokens();
		System.out.println();
		service1.printTokens();
		

	}//end main

}//end WordsManipulationClient



class WordFrequencyService
{
	String[] words = new String [300];      
	int count;


	public void readPrintText()
	{
		
	
		Scanner scan = new Scanner(System.in);
		
		Scanner fileScan;
		boolean validName = false;
		
		
		 do
         {
           
            System.out.print("Enter file name: ");
            String str1 = scan.nextLine(); 
         
            try
            {
            	
               fileScan = new Scanner (new File(str1));
               validName = true;
               
               int lineCount =0;
		
               System.out.println("\tReport1 - Text in Data File");
               System.out.println();
               System.out.println("-------------------------------------------------------");
               while (fileScan.hasNextLine( ))
                   
               {
                 
                  
                  String str = fileScan.nextLine();
                  System.out.println(str);
                  lineCount++;
                 
                  
		       }//end while
               
               System.out.println();
               System.out.println("Number of Lines Read = " + lineCount);
               System.out.println("-------------------------------------------------------");
               fileScan.close();
            }//end try
               
               catch(FileNotFoundException fnfe)
               {
                  System.out.println("Invalid File name; enter again");
               }
         }while(!validName);
		 
	}//end ReadPrintText();
	
	
	public void storeTokens()
	{
		
		Scanner scan = new Scanner(System.in);
		
		Scanner fileScan;
		boolean validName = false;
		
		
		 do
         {
           
            System.out.print("Enter file name: ");
            String str1 = scan.nextLine(); 
         
            try
            {
            	
               fileScan = new Scanner (new File(str1));
               validName = true;
               
               int count = 0;
               
               while(fileScan.hasNext())
               {
            	   
            	   String str = fileScan.nextLine();
            	   StringTokenizer tokens = new StringTokenizer(str,",;.");;
            	   while(count<300 && tokens.hasMoreTokens())
            	   {
            		   String aToken = tokens.nextToken();
            		   words[count] = aToken;
            		   count++;
            		 
            		 
            	   }
            	  
               }
               

            
               fileScan.close();
            }//end try
            
            catch(FileNotFoundException fnfe)
            {
               System.out.println("Invalid File name; enter again");
            }
      }while(!validName);
		  
	}//end storeTokens
   
	 
	 public void printTokens()
	 {
		 System.out.println();
		 System.out.println("\tReport2 - Words without punctuation");
        System.out.println();
        System.out.println("-------------------------------------------------------");
		 
        
       for(int i=0; i<count; i++)
       {
        	if(i % 5 == 0)
        	
        	System.out.println(count);
        	
       
       }
        	
    
        	
        
        
		 
        System.out.println();
        System.out.println("-----------------------------------------------------------");
	 }
	 
	 
	 
}
      
	
	



Is This A Good Question/Topic? 0
  • +

Replies To: Manipulating words from the file

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Manipulating words from the file

Posted 06 October 2012 - 05:54 AM

Dunno what exactly you're looking for, but maybe something like this:
for(int i = 0; i < words.length; i++)
{
   if(i % 5 == 0)
   {
	  System.out.println();
   }
   System.out.print(words[i] + " ");
}

Was This Post Helpful? 1
  • +
  • -

#3 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Re: Manipulating words from the file

Posted 12 October 2012 - 01:22 PM

Okay that makes sense thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1