Feinschliff und Strukturierung der User Inputs, als auch verbesserte Eingabe der Datei

main
HausCook 2026-11-16 09:11:22 +01:00
parent 56ba0ed661
commit 36fe58154e
4 changed files with 100 additions and 43 deletions

View File

@ -1,24 +1,45 @@
import java.lang.classfile.constantpool.IntegerEntry;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Analyser {
public static ArrayList<Integer> countElements(ArrayList<String> uniqueWordList,ArrayList<String> allWordsList ) {
public static ArrayList<Integer> countElements(ArrayList<String> uniqueWordList, ArrayList<String> allWordsList) {
ArrayList<Integer> individualWordCount = new ArrayList<>();
for (int i = 0; i<uniqueWordList.size(); i++) {
for (int i = 0; i < uniqueWordList.size(); i++) {
individualWordCount.add(0);
}
for (String word : allWordsList) {
int wordCount = individualWordCount.get(uniqueWordList.indexOf(word));
individualWordCount.set(uniqueWordList.indexOf(word),++wordCount);
for (String word : allWordsList) {
int wordCount = individualWordCount.get(uniqueWordList.indexOf(word));
individualWordCount.set(uniqueWordList.indexOf(word), ++wordCount);
}
return individualWordCount;
}
public static String[] frequentWords(ArrayList<String> uniqueWordList, ArrayList<Integer> wordCounts) {
ArrayList<String> wordsCopy = new ArrayList<>(uniqueWordList);
ArrayList<Integer> countsCopy = new ArrayList<>(wordCounts);
String[] frequentWords = new String[10];
}
for (int i = 0; i < frequentWords.length; i++) {
if (wordsCopy.isEmpty()) {
break;
}
int highestFrequency = 0;
int highestIndex = 0;
for (int j = 0; j < wordsCopy.size(); j++) {
if (countsCopy.get(j) > highestFrequency) {
highestFrequency = countsCopy.get(j);
highestIndex = j;
}
}
frequentWords[i] = wordsCopy.get(highestIndex);
countsCopy.remove(highestIndex);
wordsCopy.remove(highestIndex);
}
return frequentWords;
}
}

View File

@ -1,20 +1,58 @@
public static void main(String[] args) {
import java.util.ArrayList;
final String filePath = "C:\\Users\\Toni G\\IdeaProjects\\GitPeerGroup\\src\\BeeMovieScript.txt";
public class Main {
final ArrayList<String> fileLines = TextReader.readFileLines(filePath);
final ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
final ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
final ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords,fileWords);
IO.println(wordCounts);
IO.println(uniqueWords);
printWords(uniqueWords, wordCounts);
public static void main(String[] args) {
ArrayList<String> fileLines = null;
while (!TextReader.isRealFile) {
String file = IO.readln("Enter the name of the file: ");
final String filePath = "C:\\Users\\Toni G\\IdeaProjects\\GitPeerGroup\\src\\Textfiles\\" + file;
fileLines = TextReader.readFileLines(filePath);
}
}
ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords, fileWords);
static void printWords(ArrayList<String> uniqueWords, ArrayList<Integer> wordCounts) {
for (String word : uniqueWords) {
System.out.println(word + " = " + wordCounts.get(uniqueWords.indexOf(word)));
boolean running = true;
while (running) {
IO.println("\nText Viewer ");
IO.println("1: Total Word Count");
IO.println("2: All Words and their Frequency");
IO.println("3: Top 10 Words");
IO.println("4: Exit");
String choice = IO.readln("Pick a Number [1. |2. |3. |4. ]");
switch (choice) {
case "1" -> {
IO.println("Woerter gesamt: " + fileWords.size());
IO.println("Unterschiedliche Woerter: " + uniqueWords.size());
}
case "2" -> printWords(uniqueWords, wordCounts);
case "3" -> {
String[] frequentWords = Analyser.frequentWords(uniqueWords, wordCounts);
printFrequentWords(frequentWords);
}
case "4" -> running = false;
default -> IO.println("Ungueltige Eingabe.");
}
}
}
static void printWords(ArrayList<String> uniqueWords, ArrayList<Integer> wordCounts) {
for (String word : uniqueWords) {
System.out.println(word + " = " + wordCounts.get(uniqueWords.indexOf(word)));
}
}
static void printFrequentWords(String[] frequentWords) {
for (int i = 0; i < frequentWords.length; i++) {
if (frequentWords[i] != null) {
System.out.println((i + 1) + ". " + frequentWords[i]);
}
}
}
}

View File

@ -1,55 +1,53 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Scanner;
public class TextReader {
static boolean isRealFile = false;
Scanner sc = new Scanner(System.in);
public static ArrayList<String> readFileLines(String filePath) {
public static ArrayList<String> readFileLines(String filePath) {
ArrayList<String> lines = new ArrayList<>();
File file = new File (filePath);
File file = new File(filePath);
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
try (Scanner scanner = new Scanner(file)) {
isRealFile = true;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lines.add(line);
}
} catch (FileNotFoundException e) {
System.out.println("es gibt keine datei im angegebenen dateipfad: "+filePath);
} catch (FileNotFoundException e) {
System.out.println("es gibt keine datei im angegebenen dateipfad: " + filePath);
}
return lines;
}
public static ArrayList<String> linesToWords(ArrayList<String> lines) {
public static ArrayList<String> linesToWords(ArrayList<String> lines) {
ArrayList<String> words = new ArrayList<>();
for (String line : lines) {
for (String line : lines) {
String[] currentLine = line.split(" ");
for (String word : currentLine) {
for (String word : currentLine) {
for (int i = 0; i < word.length(); i++) {
word = word.replace(",", "")
.replace(".", "")
.replace("!", "")
.replace("?", "")
.replace("\"", "");
}
words.add(word);
}
words.add(word);
}
}
return words;
}
public static ArrayList<String> uniqueWordList(ArrayList<String> wordList) {
public static ArrayList<String> uniqueWordList(ArrayList<String> wordList) {
ArrayList<String> uniqueWords = new ArrayList<>();
for(String word : wordList) {
if (!uniqueWords.contains(word)) {
for (String word : wordList) {
if (!uniqueWords.contains(word)) {
uniqueWords.add(word);
}
}
return uniqueWords;
}
}
}