import java.util.Scanner; public class Vokalersetzung { /* Übungsblatt 1 - 14.10.2015 Aufgabe 1.7 (H) Vokale ersetzen */ public static void main(String[] args) { String text = "Hat der alte Hexenmeister\n" + "sich doch einmal wegbegeben!\n" + "Und nun sollen seine Geister\n" + "auch nach meinem Willen leben.\n" + "Seine Wort und Werke\n" + "merkt ich und den Brauch,\n" + "und mit Geisterstärke\n" + "tu ich Wunder auch.\n" + "Walle! walle\n" + "Manche Strecke,\n" + "daß, zum Zwecke,\n" + "Wasser fließe\n" + "und mit reichem, vollem Schwalle\n" + "zu dem Bade sich ergieße.\n"; // all vowels (characters to be replaced) in lowercase (important!) char[] vowel = {'a', 'e', 'i', 'o', 'u'}; // Ask for the vowel to replace the others. System.out.println("Mit welchem Vokal sollen die anderen Vokale ersetzt werden?"); // Read replacement vowel from console (has to be a vowel!). int replaceWith; // the index of the vowel that replaces the others // (Info: Instead of simply using the entered character, which // would also only be a vowel when leaving the loop, the index // of the vowel in array (vowel) is used. There is no difference, // how it is done in the script. This way is only used for formal // reasons, but one discrete variable would be used anyway.) Scanner input = new Scanner(System.in); // scanner for reading from console Outerloop: // to exit the while loop (no separate help var required) while (true) { System.out.print("Eingabe: "); // Read string entered on the console and check length (1 = char). String inputString = input.next().toLowerCase(); if (inputString.length() > 1) { System.out.println("Die Eingabe ist zu lang!"); continue; // skip the rest of the loop } // Check if the entered char is one of the required ones (vowels). for (replaceWith = 0; replaceWith < vowel.length; replaceWith++) { if (inputString.charAt(0) == vowel[replaceWith]) break Outerloop; } // If this point is reached, something uncorrect was entered. System.out.println(inputString + " ist kein Vokal!"); } // Print replacement character System.out.println("Vokale wurden durch " + vowel[replaceWith] + " ersetzt:"); System.out.println(); // blank line // Replace characters (upper and lower case) for (int i = 0; i < vowel.length; i++) { if (i != replaceWith) { // vowel does not need to replace itself text = text.replace(vowel[i], vowel[replaceWith]); text = text.replace(Character.toUpperCase(vowel[i]), Character.toUpperCase(vowel[replaceWith])); } } System.out.println(text); // Print new text } }