05_Praktikum-2_Wortstatistik/Main.java

43 lines
1.5 KiB
Java
Raw Normal View History

2021-10-28 17:16:26 +02:00
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Worthaeufigkeitsanalyse analyse = new Worthaeufigkeitsanalyse();
File file = getFileFromArguments(args);
String text = "";
Scanner scanner = null;
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()){
text = text + scanner.nextLine();
text = text + " ";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
analyse.verarbeiteText(text);
analyse.druckeStatistik();
}
private static File getFileFromArguments(String[] args) {
// ------------------------------------------------------------
// Check if a valid filename has been supplied on command line:
// ------------------------------------------------------------
// Is there a command line argument at all?
if (args.length <= 0) {
throw new IllegalArgumentException("ERROR: You have to supply a filename on the command line!");
}
// Yes, there is at least one argument:
String filename = args[0];
File file = new File(filename);
if (!file.exists()) { // Check if file with that name exists:
throw new RuntimeException(String.format("ERROR: Data file: '%s' does not exist!", filename));
}
return file;
}
}