Praktische Informatik I Tutorium René Frank Universität Marburg November 29, 2011
searchandreplace(sourcestr, searchstr, replacestr) Aufgabe: im String sourcestr jegliches vorkommen der Zeichenkette searchstr durch String replacestr ersetzen bereits bekannte nützliche Methoden: String var = "abcd" var.charat(1) --> b var.substring(1,3) --> "bc" weitere unter: http://docs.oracle.com/javase/7/docs/api/index.html
Einfache Lösung mit replace() String text = "dies ist ein Beispiel"; System.out.println(text.replace("e", "xx")); Output => dixxs ist xxin Bxxispixxl sämtliche replace-methoden dürfen nicht verwendet werden Null-Werte als Parameter beachten!!! Was ist wenn searchstr == StringBuilder() hilfreich append() effizenter als + Skript 7-10/11
filternumbers(str) Reguläre Ausdrücke Skript 7/14ff in Java-API die Klassen Pattern, Matcher durchstöbern Zahlen aus String filtern mittels regex folgendes soll die Methode leisten: String temp = "Zahlen abc -22 0.7 sollen444gefiltert werden"; filternumbers(temp); Output => {-22.0, 0.7, 444.0} Die Umwandlung von String zu double double b = Double.parseDouble("123");
filterwords(str) Analog zur vorherigen Aufgabe Wörter aus einem String filtern mittels regex ein Bindestrich als Sonderzeichen ist erlaubt Rückgabe der Wörter als String-Array folgendes soll die Methode leisten: String temp = "Konzert-Karten--Verkauf abc"; filternumbers(temp); Output => {"Konzert-Karten", "Verkauf", "abc"}
setprecedencebrackets Arithmetic(str) Ausdruck mit Operatoren +, -, *, / und % soll korrekt geklammert werden StringBuilder() hilfreich (append(), insert() etc.) String durchlaufen, Operator suchen und mit nächsten Operator vergleichen geeignete Hilfmethoden sind von Vorteil 6+5 4 3/2%1 6+(5 4) (3/2%1)
Anweisungen System.out.println(++y * 2 + (x + --y) / y++ + x); System.out.println(y ((x & (y - x)) ^ x)); System.out.println(y < x? y:((x ^ (y - x) & x))); int x = 1, y = 2; System.out.println( (x << 3 * ++y) >> y ); int ai = 2; double bd = 3.0, ad = 1.0; System.out.println(ai += ai < (bd + ad) / 2? ad : bd); boolean bb = false; System.out.println(bb = 16 >> 1 >>> 2 % 2 == 8); Ausgabe
Anweisungen Ausgabe System.out.println(++y * 2 + (x + --y) / y++ + x); 10 System.out.println(y ((x & (y - x)) ^ x)); System.out.println(y < x? y:((x ^ (y - x) & x))); int x = 1, y = 2; System.out.println( (x << 3 * ++y) >> y ); int ai = 2; double bd = 3.0, ad = 1.0; System.out.println(ai += ai < (bd + ad) / 2? ad : bd); boolean bb = false; System.out.println(bb = 16 >> 1 >>> 2 % 2 == 8);
Anweisungen Ausgabe System.out.println(++y * 2 + (x + --y) / y++ + x); 10 System.out.println(y ((x & (y - x)) ^ x)); 3 System.out.println(y < x? y:((x ^ (y - x) & x))); int x = 1, y = 2; System.out.println( (x << 3 * ++y) >> y ); int ai = 2; double bd = 3.0, ad = 1.0; System.out.println(ai += ai < (bd + ad) / 2? ad : bd); boolean bb = false; System.out.println(bb = 16 >> 1 >>> 2 % 2 == 8);
Anweisungen Ausgabe System.out.println(++y * 2 + (x + --y) / y++ + x); 10 System.out.println(y ((x & (y - x)) ^ x)); 3 System.out.println(y < x? y:((x ^ (y - x) & x))); 1 int x = 1, y = 2; System.out.println( (x << 3 * ++y) >> y ); int ai = 2; double bd = 3.0, ad = 1.0; System.out.println(ai += ai < (bd + ad) / 2? ad : bd); boolean bb = false; System.out.println(bb = 16 >> 1 >>> 2 % 2 == 8);
Anweisungen Ausgabe System.out.println(++y * 2 + (x + --y) / y++ + x); 10 System.out.println(y ((x & (y - x)) ^ x)); 3 System.out.println(y < x? y:((x ^ (y - x) & x))); 1 int x = 1, y = 2; System.out.println( (x << 3 * ++y) >> y ); 64 int ai = 2; double bd = 3.0, ad = 1.0; System.out.println(ai += ai < (bd + ad) / 2? ad : bd); boolean bb = false; System.out.println(bb = 16 >> 1 >>> 2 % 2 == 8);
Anweisungen Ausgabe System.out.println(++y * 2 + (x + --y) / y++ + x); 10 System.out.println(y ((x & (y - x)) ^ x)); 3 System.out.println(y < x? y:((x ^ (y - x) & x))); 1 int x = 1, y = 2; System.out.println( (x << 3 * ++y) >> y ); 64 int ai = 2; double bd = 3.0, ad = 1.0; System.out.println(ai += ai < (bd + ad) / 2? ad : bd); 5 boolean bb = false; System.out.println(bb = 16 >> 1 >>> 2 % 2 == 8);
Anweisungen Ausgabe System.out.println(++y * 2 + (x + --y) / y++ + x); 10 System.out.println(y ((x & (y - x)) ^ x)); 3 System.out.println(y < x? y:((x ^ (y - x) & x))); 1 int x = 1, y = 2; System.out.println( (x << 3 * ++y) >> y ); 64 int ai = 2; double bd = 3.0, ad = 1.0; System.out.println(ai += ai < (bd + ad) / 2? ad : bd); 5 boolean bb = false; System.out.println(bb = 16 >> 1 >>> 2 % 2 == 8); true