VL-Programmieren/VL12/Aufgabe02/Aufgabe02.java

69 lines
2.0 KiB
Java

package VL12.Aufgabe02;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Vorlesung 12 / Aufgabe 2
*
* @author Sebastian Brosch
*/
public class Aufgabe02 {
public static void main(String[] args) {
try {
int numberOfWords = 0;
int numberOfNumbers = 0;
StringReader stringReader = new StringReader(getInputString(getPathFromPackage() + "/input/input.txt"));
StreamTokenizer streamTokenizer = new StreamTokenizer(stringReader);
while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch (streamTokenizer.ttype) {
case StreamTokenizer.TT_WORD:
numberOfWords++;
System.out.printf("ZEICHENKETTE:\t%s\n", streamTokenizer.sval);
break;
case StreamTokenizer.TT_NUMBER:
numberOfNumbers++;
System.out.printf("ZAHL:\t\t%.0f\n", streamTokenizer.nval);
break;
}
}
System.out.printf("Gelesene Zahlen: %d\n", numberOfNumbers);
System.out.printf("Gelesene Zeichenketten: %d\n", numberOfWords);
System.out.printf("Insgesamt gelesene Token: %d\n", (numberOfNumbers + numberOfWords));
} catch (Throwable e) {
;
}
}
/**
* Method to get the content of a file as a string.
*
* @param inputPath The path of the file.
* @return The content of the file or an empty string if the file is not
* available.
*/
private static String getInputString(String inputPath) {
try {
return new String(Files.readString(Paths.get(inputPath)));
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
/**
* Method to get the relative path based on the package name.
*
* @return The relative path of the package based on the package name.
*/
private static String getPathFromPackage() {
return Aufgabe02.class.getPackageName().replace(".", "/");
}
}