diff --git a/GDP_Gesamt/src/Uebungen/Uebung5/Aufgabe2.java b/GDP_Gesamt/src/Uebungen/Uebung5/Aufgabe2.java index 5f73569..9d739ca 100644 --- a/GDP_Gesamt/src/Uebungen/Uebung5/Aufgabe2.java +++ b/GDP_Gesamt/src/Uebungen/Uebung5/Aufgabe2.java @@ -1,5 +1,6 @@ package Uebungen.Uebung5; +import java.awt.desktop.SystemSleepEvent; import java.util.Scanner; import java.lang.Character; import java.util.Arrays; @@ -19,51 +20,73 @@ public class Aufgabe2 { Scanner scanner = new Scanner(System.in); System.out.print("Enter desired string: "); char[] stringAsCharArr = scanner.nextLine().toCharArray(); - char[] usedCharArr = usedChars2(stringAsCharArr); - for(char character:usedCharArr) System.out.println(" \""+ character + "\""); // Debug - // System.out.println(stringAsCharArr.hashCode()); // Get the hashcode of the array + stringAsCharArr = normalize(stringAsCharArr); + char[] usedCharArr = usedChars(stringAsCharArr); + int[] charAmountArr = countChars(usedCharArr ,stringAsCharArr); + histogramm(20,usedCharArr, charAmountArr); } - public static char[] usedChars(char[] charArray){ - char[] workingArray = new char[charArray.length]; - int position = 0; // current position - for(char currChar:charArray) { - currChar=Character.toLowerCase(currChar); //normalize - boolean exists=false; - for(char targetChar:workingArray) if(targetChar==currChar) exists=true; - if(!exists){ - workingArray[position]=currChar; - position++; - } - } - char[] targestArray = new char[position]; - for(int i=0;iwidth) + for(int j=0;jlargestNumber) + largestNumber = inputArray[i]; + return largestNumber; + } + public static int[] countChars(char[] availableChars, char[] scannedArray){ + int[] occurrences = new int[availableChars.length]; + // Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) [...] For type int, the default value is zero, that is, 0. + for(char currChar:scannedArray) + for(int i=0;i increment counter // now remove all similar chars from that point till the end of the array - for (int j = i + 1; j < workingArray.length; j++) { - if (workingArray[i] == workingArray[j]) workingArray[j] = (char) 0; // set the char to "Null character" - } + for (int j = i + 1; j < workerArray.length; j++) // loop through starting from current pos. + if (workerArray[i] == workerArray[j]) // check if char already exists + workerArray[j] = (char) 0; // set the char to "Null character" } } char[] targetArray = new char[counter]; // create new int targetPosition = 0; // initiate counter for the Target Array - for(char currChar:workingArray){ // seek through all chars + for(char currChar:workerArray) // seek through all chars if((int)currChar!=0){ // if its not a null character it is a new char targetArray[targetPosition] = currChar; // add the current char targetPosition++; // we added a new char, so we need to increment for the next one } - } + return targetArray; } }