I have a few questions about my current program and i will attach the word for word assignment (so there's no confusion) and my progress.
Assignment:
Write a program that asks the user to enter the name of a text file and then displays the line count, word(token) count and the character(non-delimeter) count in the file in a nice format. Use the StringTokenizer class in the java.util pacjkage to parse the tokens from the input lines. In addition to the whitespace characters, question mark(?), double quotes("), commas(,) and periods(.) should also be treated as delimeters and thus not to be counted as tokens or parts of tokens. Use the sample text file input5.txt as an input to your program.
input5.txt
Little miss Muffet,
sat on a tuffet,
eating her candy away.
Along came a spider,
who sat down beside her,
and said "Will you marry me?"
My code thus far:
import java.io.*;
import java.util.*;
public class Project5
{
public static void main(String[] args) throws IOException
{
Scanner inputStream = null;
PrintWriter outputStream = null;
System.out.println("Please enter a file name");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.nextLine();
try
{
inputStream = new Scanner(new FileInputStream(fileName));
outputStream = new PrintWriter(new FileOutputStream("newinput5.txt"));
}
catch(FileNotFoundException e)
{
System.out.println("Error");
System.exit(0);
}
String wordCount = null;
int count = 0;
String lineCount = null;
int count1 = 0;
while(inputStream.hasNext())
{
wordCount = inputStream.next();
count ++;
}
while(inputStream.hasNext())
{
lineCount = inputStream.nextLine();
count1 ++;
}
outputStream.println("Word count:" +count);
outputStream.println("Line count:"+count1);
//outputStream.println("Character count:"+charCount)
System.out.println("writing to file");
inputStream.close();
outputStream.close();
}
}
my ouput (with my keyboard input):
Please enter a file name
input5.txt
writing to file
my file ouput after the file is written:
Word count:26
Line count:0
my questions and comments:
I am a little confused about writing the input and output. Should i reenter the input5.txt (the 6 line poem) into the code as ouputSream.println() statements, or do i just pre-write it like i have so that i may access it in this program?
I believe i only need to use the StringTokenizer class on char because it seems as if the scanner class methods are suitable for the lines and words, is this correct? How would i go about starting the char tokenization? My line count is wrong and i tried a nested while loop with no avail. Would StringTokenizer come in handy for char and line or just char? Thank you very much.

New Topic/Question
Reply



MultiQuote








|