Adressüberprüfung

K

Klickmichfan

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
 
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:

Java:
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);
		}
	}
}
 
Zurück