The Artima Developer Community
Sponsored Link

Java Answers Forum
Can anyone tell me where I went wrong??

3 replies on 1 page. Most recent reply: Mar 24, 2002 9:21 PM by Hoody

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Can anyone tell me where I went wrong?? Posted: Mar 21, 2002 7:34 PM
Reply to this message Reply
Advertisement
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.


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;

public class ReadWriteFileAsTokens

{ public static void 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, "|");
String word = tokenizer.nextToken();
out.println(inputLine);
}
}
// 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();
}


}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Can anyone tell me where I went wrong?? Posted: Mar 21, 2002 7:59 PM
Reply to this message Reply
Well at first blush, it looks like you went wrong in not using the java tags to post your code in a readable format...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Can anyone tell me where I went wrong?? Posted: Mar 21, 2002 8:13 PM
Reply to this message Reply
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;
public class ReadWriteFileAsTokens
{
    public static void 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).

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Can anyone tell me where I went wrong?? Posted: Mar 24, 2002 9:21 PM
Reply to this message Reply
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?

Flat View: This topic has 3 replies on 1 page
Topic: Sorry... Newbie print question updated Previous Topic   Next Topic Topic: Why doesn't this print?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use