ERLEDIGT
NEIN
NEIN
ANTWORTEN
3
3
ZUGRIFFE
508
508
EMPFEHLEN
-
28.12.09 16:25 #1Klickmichfan Tutorials.de Gastzugang
Hallo,
ich habe ein Problem!
Und zwar lese ich von der Konsole eine Adresse aus (Str. und Hausnummer), als String.
Jetzt muss aber eine Überprüfung stattfinden, ob es Buchstaben und Zahlen beinhaltet.
Das letztere habe ich schon:
public static String strasseUeberpruefen(String strasse){
for (int i=1; i<10; i++){
if(strasse.contains("" + i)){
return strasse;
}
}
System.out.println("==> Bitte auch eine Hausnummer angeben. <==");
return strasseUeberpruefen(lesenVonDerKonsole("Bitte geben Sie die Straße mit Hausnummer ein."));
}
wie kann ich das mit Buchstaben überprüfen?
Danke für eure Antworten, jetzt schonmal!
MfG
Klickmichfan
-
29.12.09 10:05 #2
Moin,
dafür gibt es die Funktionen "isDigit" und "isLetter" !
Schau mal hier:
http://openbook.galileodesign.de/jav...42aa001583b3c6
Guten Rutsch
Gruß
Klaus
BTW: Nutze bitte die Code-Tags zum Posten von Code-Schnipseln - Danke !!Es ist noch kein Meister vom Himmel gefallen - sonst hätte man schon längst seine Leiche gefunden !!
Falls ich helfen konnte, wäre eine Bewertung oder ein Danke nett ;-)
-------------------------------------------------------------------------------------------------
Ich beantworte keine Fragen per PN !!
Stellt Eure Fragen im Forum - dann haben alle etwas davon !!
-
04.01.10 17:00 #3
Wie währs mit regular Expressions?
Einfach prüfen ob der String dem Muster "Buchstaben Zahlen" entspricht?
näheres hier
MfG
-
04.01.10 20:23 #4
Und zu guter Letzt könnte es ja noch per Array/Schleife gemacht werden.
Denke es ist einfacher als RegEx., und komplett auf jedes Bedürfniss zuschneidbar:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
public class Test { private static final char[] LEGAL_LETTER = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'ß', ' ', '-' }; private static final char[] LEGAL_NUMBER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; public static void main(String[] args) { String address = "Wiener Straße"; int doorNumber = 58; int zip = 2345; String place = "Brunn/Geb."; place = place.replaceAll("Geb.", "Gebirge"); //Typische abkürzungen Filtern place = place.replaceAll("/", " am "); place = place.replaceAll(".", ""); checkAddress(address); checkAddress(doorNumber); checkAddress(zip); checkAddress(place); } private static void checkAddress(String s, char[] legalCharacters) throws FormatException { char[] chars = s.toCharArray(); for(char c:chars) if(!charInArray(legalCharacters, c)) throw new FormatException(); } private static void checkAddress(String s) throws FormatException { checkAddress(s, LEGAL_LETTER); } private static void checkAddress(int i) throws FormatException { checkAddress(String.valueOf(i), LEGAL_NUMBER); } private static boolean charInArray(char[] array, char c) { boolean result = false; for(byte b = 0, size = (byte)array.length; ((b < size) && (!result)); b++) if(array[b] == c) result = true; return(result); } private static class FormatException extends IllegalArgumentException { private static final long serialVersionUID = 1L; private FormatException() { super(); } private FormatException(String s) { super(s); } } }





Zitieren
Login





