so far
but I have some trouble with the case ' '
public class WordCount {
private int numChars;
private int numWords;
private int numLines;
public WordCount()
{
numChars = 0;
numWords = 0;
numLines = 0;
}
public void wordCount(String f)
{
try
{
FileReader reader = new FileReader(f);
Scanner in = new Scanner (reader);
int lineAt = 1;
while(in.hasNextLine())
{
String line = in.nextLine();
int lineReade = line.length(); // read the string and count
lineRead += lineRead +1;
for(int j = 0; j+1 <lineRead; j++)
{
final char charValue = line.charAt(j);
if (charValue == ' ' && charValue != line.charAt(j+1))
numWords++;
if(charValue == '\n' && charValue != line.charAt(j+1))
numWords++;
}
numlines++;
lineAt++;
}
catch (FileNotFoundException e)
{
System.out.println("Error" + e);
}
}
}
I think this is implemented right but i get a
error message att ' ' char
my main method needs
to print in the terminal one of these commands
-l number of lines
-w number of words
-n number of chars
with a filename
if the user doesent print any of these commands it just
gives them all.
but im not sure how to do this
so it would for example be in terminal window:
-w test.txt
and it would print out the sum of all the words in that file
my main class so far is:
public class WordCountMain{
public static void main (String [] args) {
Scanner console = new Scanner(System.in);
System.out.println("OPTION" +
" -n print the chars" +
" -l print the lines" +
" -w print the words");
System.out.print("enter a file name and a Option");
String inputFileName = console.next();
WordCount w = new WordCount();
w.wordCount(inputFileName);
System.out.print(w.getNumberOfLines()+""+w.getCharNumber()+""+w.getWordNumber()+""+inputFileName);
}
}
[/code]


Sign In
Create Account

Back to top









