To be honest...I am horrible at java. I just got a job and have missed class a few times because of it and im supposed to create a project that can read the number of lines, words, characters, average words length, and letters in a file. I am trying to get the character reader to work right now but it tells me that I only get 1 character...
what am I doing wrong?
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class TextStatistics
{
int wordCount;
int lineCount;
int charCount;
public void wordcounter(String filename) throws IOException, NullPointerException
{
File inFile = new File(filename);
try { Scanner file = new Scanner(inFile);
while ((file.hasNext()) )
{
String s = file.next();
wordCount++;
}
} catch (IOException e) {
}
System.out.println("\n" + wordCount + " words read");
}
public void lineCounter(String filename) throws IOException, NullPointerException
{
File inFile = new File(filename);
try { Scanner file = new Scanner(inFile);
while ((file.hasNext()) )
{
String t = file.nextLine();
lineCount++;
}
} catch (IOException e) {
}
System.out.println("\n" + lineCount + " lines read");
}
public void charCounter(String filename) throws IOException, NullPointerException
{
File inFile = new File(filename);
try { Scanner file = new Scanner(inFile);
StringTokenizer tokenizer = new StringTokenizer(filename, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n\t");
while (tokenizer.hasMoreTokens())
tokenizer.nextToken();
charCount++;
}
catch (IOException e) {
}
System.out.println("\n" + charCount + " lines read");
}
@Override
public String toString() {
return "TextStatistics [chars=" + charCount + ", lines=" + lineCount
+ ", words=" + wordCount + "]";
}
}