i am supposed touse the string tokenizer so i can separte each word i put into the input file so it prints one word per line
// Reads text from an input file one line at a time, breaks the line into words // Then prints each word to another file, one word per line. // from an input file.
// Open input and output files try { reader = new FileReader(inFileName); writer = new FileWriter(outFileName); } catch(FileNotFoundException e) { System.err.println("Cannot find input file "); System.exit(1); // abnormal termination status code } catch(IOException e) { System.err.println("Cannot open input/output file " ); System.exit(2); }
// Set up to read a line and write a line BufferedReader in = new BufferedReader(reader); PrintWriter out = new PrintWriter(writer);
// Read a line from one file and write to the other out.println("Copied file is:");
boolean done = false; while(!done)
{ String inputLine = in.readLine();
if (inputLine == null) { done = true; } else { StringTokenizer tokenizer= new StringTokenizer(inputLine, "|"); String word = tokenizer.nextToken(); out.println(inputLine); } } // end of input // initialize a tokenizer and attach it to a line
You needed to use the hasMoreElements() and getNextElement() in a loop, like so:
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
publicclass ReadWriteFileAsTokens
{
publicstaticvoid main(String[] args)
throws IOException
{
// Initializations
FileReader reader = null;
FileWriter writer = null;
String inFileName = "TokensIn.txt";
String outFileName = "TokensOut.txt";
// Open input and output files
try
{
reader = new FileReader(inFileName);
writer = new FileWriter(outFileName);
}
catch(FileNotFoundException e)
{
System.err.println("Cannot find input file ");
System.exit(1); // abnormal termination status code
}
catch(IOException e)
{
System.err.println("Cannot open input/output file ");
System.exit(2);
}
// Set up to read a line and write a line
BufferedReader in = new BufferedReader(reader);
PrintWriter out = new PrintWriter(writer);
// Read a line from one file and write to the other
out.println("Copied file is:");
boolean done = false;
while(!done)
{
String inputLine = in.readLine();
if(inputLine == null)
done = true;
else
{
StringTokenizer tokenizer = new StringTokenizer(inputLine, " ");
while( tokenizer.hasMoreElements() )
out.println( (String)tokenizer.nextElement() );
}
}
// end of input
// initialize a tokenizer and attach it to a line
// Close files
try
{
in.close();
}
catch(IOException e)
{
System.err.println("Error closing file.");
}
finally
{
out.close();
}
}
}
There are still some problems you should fix before packaging this up and stamping on the $99.95 SRP: - Don't use hard-coded file names! Get them from the command line. - Don't do everything in main(). main() should be lean and mean and all the real work should be in methods with names that indicate what they are doing. - main() shouldn't throw an exception! That is exceptionally sloppy and lazy! You should handle the exception and do something reasonable (like display a helpful syntax description, for instance).
i was just fixing up the program and i was haveing a problem storing the words into a one dimensional array and not accepting duplicates...does anyone know an easy way to do this?