I want to try and test this method for license-plates, but I can't seem to get the test in the main to work. Could someone help me out? The method should look like this according to my exercise instructions. This is my code:
public class RegNumber {
public static boolean isCorrectLicensPlate (String regNbr, Boolean isPrivate) {
String generalPlate = "ABCDEFGHJKLMNOPRSTUWXYZX";
String privatePlate = "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ 123456789";
if (isPrivate) {
if (regNbr.length() < 2 && regNbr.length() > 7) return false;
for(int i = 0; i < regNbr.length(); i++) {
if(!privatePlate.contains(Character.toString(regNbr.charAt(i))) ) return false;
}
}
else {
if (regNbr.length() != 7) return false;
if (regNbr.substring(3,4).equals(" ")) return false;
String letters = regNbr.substring(0, 3);
for (int i = 0; i < letters.length(); i++) {
if(!generalPlate.contains(Character.toString(letters.charAt(i)))) return false;
}
String numbers = regNbr.substring(4);
for (int i = 0; i < numbers.length(); i++) {
if (!Character.isDigit(numbers.charAt(i))) return false;
}
}
return true;
}
public static void main(String[] args) {
RegNumber asd = new RegNumber();
asd.isCorrectLicensPlate(123, false);
}
}
Aucun commentaire:
Enregistrer un commentaire