I'm making a program that asks for a 5 digit string, containing a letter, then a "-" and 3 numbers.( eG: A-123) I'm using split with the delimiter "-" to split the letter and the numbers, but the whole thing falls apart if the input is different than that exact format. So my question is how to block out any input that doesn't fit with the specific format.
the code I'm using so far:
public Room(String Combo) {
if (Combo.length() == 5){
String delimiter = "-";
String[] temp = Combo.split(delimiter);
long FloorRoomNo = Integer.parseInt(temp[1]);
long Floor = FloorRoomNo/100;
long RoomNo = FloorRoomNo/100;
this.Floor = (int)Floor;
this.RoomNo = (int)RoomNo;
Buildning = temp[0]:
}else{
System.err.println("Wrong input length");
System.exit(-1);
}
}
Combo.contains(delimiter)before you make the split. (And please change it tocombowith lowercase letter)