Computer Science Canada

Manipulating text from the file

Author:  ast_993 [ Fri Oct 05, 2012 4:30 pm ]
Post subject:  Manipulating text from the file

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<words.length; i++)
{


System.out.println(words[count]);

}

System.out.println();
System.out.println("-----------------------------------------------------------");
}



}

Author:  Tony [ Fri Oct 05, 2012 5:05 pm ]
Post subject:  Re: Manipulating text from the file

ast_993 @ Fri Oct 05, 2012 4:30 pm wrote:
not sure if the loop is properly set up.

well... does it "print the text from the file in columns of 5 words each per line"?

Author:  ast_993 [ Fri Oct 05, 2012 5:10 pm ]
Post subject:  RE:Manipulating text from the file

No it prints nothing

Author:  Tony [ Fri Oct 05, 2012 10:20 pm ]
Post subject:  RE:Manipulating text from the file

that should answer your question -- something is not setup as you think you've set it up. Time to step back and verify the steps that you are taking.

Author:  QuantumPhysics [ Sat Oct 06, 2012 1:32 am ]
Post subject:  RE:Manipulating text from the file

When everything else fails. Try again. That's how you learn.


: