I have written this code, for a java word counter that takes input from single or multiple files and gives back the output for the number of words.
While exception handling, I require it to give me an error message when the execution code lacks specification of files names.
For example:
javac wordcount.java
should return an error message
and
javac wordcount.java names.java
should return the number of words.
Here is my code:
import java.io.*;
import java.util.*;
import java.text.*;
public class wordcount extends Thread {
String str;
public wordcount(String s) {
this.str = s;
}
public static void main(String args[]) throws Throwable {
for (String s: args) {
//System.out.println(s);
new wordcount(s).start();
}
}
public void run() {
System.out.println(this.str);
try {
File file = new File(this.str);
Scanner sc = new Scanner(new FileInputStream(file));
if {
System.out.println("file doesnt exist");
}
int count = 0;
while (sc.hasNext()) {
sc.next();
count++;
}
System.out.println("File :" + this.str + "=" + count);
} catch (Exception e) {
System.out.println(e);
}
}
}
Comments
Post a Comment