Vorlesung 12 / Aufgabe 2

This commit is contained in:
Sebastian Brosch 2024-05-01 23:19:04 +02:00
parent 4019f76373
commit cea993fcd1
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,62 @@
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 - Ein- und Ausgabe - Aufgabe 2
* @author Sebastian Brosch
*/
public class Aufgabe02 {
public static void main(String[] args) {
try {
int words = 0;
int numbers = 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:
words++;
System.out.printf("ZEICHENKETTE:\t%s\n", streamTokenizer.sval);
break;
case StreamTokenizer.TT_NUMBER:
numbers++;
System.out.printf("ZAHL:\t\t%.0f\n", streamTokenizer.nval);
break;
}
}
System.out.printf("Gelesene Zahlen: %d\n", numbers);
System.out.printf("Gelesene Zeichenketten: %d\n", words);
System.out.printf("Insgesamt gelesene Token: %d\n", (numbers + words));
} 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(".", "/");
}
}

View File

@ -0,0 +1,2 @@
Dieser Text hat 6
Worte und 2 Zahlen.