开发者

Trouble in getting proper string input

I am writing a method in Java in which the user is supposed to enter a license plate for a car. The fist two signs must be capital letters, the third sign must be a digit between 1 and 9, and the last 4 digits must be digits between 0 and 9. If the user does not enter this properly, an error message should appear, and the user will be asked to input the license plate again.

After testing the problem I have discovered that if I deliberately make many different mistakes over and over, and then finally enter the license plate correctly, the program still informs me that my input is wrong. I am having a hard time knowing how to construct this, since it is supposed to take into account so many possible errors. My code presently looks like this for the method in question:

    char sign;
System.out.print("License plate: ");
    licensePlate = input.next();

    for (int index = 0; index < 2; indeks++) {
        sign = licensePlate.charAt(indeks);
        while (sign < 'A' || sign > 'Z') {
            System.out.println(licensePlate + " is not a valid license plate (two big letters + five digits where the first digit can not be 0)");
            System.out.print("License plate: ");
            licensePlate = input.next(); }
    }

    while (licensePlate.charAt(2) < '1' || licensePlate.charAt(2) > '9') {
        System.out.println(licensePlate + " is not a valid license plate (two big letters + five digits where the first digit can not be 0)");
    System.out.print("License plate: ");
    licensePlate = input.next(); }

    for (int counter = 3; counter < 7; counter++) {
        sign = licensePlate.charAt(teller);
        while (sign < '0' || sign > '9') {
            System.out.println(licensePlate + " is not a valid license plate (two big letters + five digits where the first digit can not be 0)");
    System.out.print("License plate: ");
    licensePlate = input.next(); }  开发者_开发知识库
    }

    carObject.setLicensePlate(licensePlate); 

If anyone can help me writing this properly I would be extremely grateful!


The problem is that you're taking new input every so often, but then not starting again. It would be worth having a separate method to perform the test, like this:

boolean gotPlate = false;    
String plate = null;

while (!gotPlate) {
    System.out.print("License plate: ");
    plate = input.next();
    gotPlate = checkPlate(plate);
}
carObject.setLicensePlate(plate);

Now put the rest of your logic into the checkPlate method:

static boolean checkPlate(String plate) {
    // Fixed typos here, by the way...
    for (int index = 0; index < 2; index++) {
        char sign = plate.charAt(index);
        if (sign < 'A' || sign > 'Z') {
            System.out.println(plate + " is not a valid license plate " + 
               "(two big letters + five digits where the first digit" + 
               " can not be 0)");
            return false;
        }
    }
    // Now do the same for the next bits...


    // At the end, if everything is valid, return true
    return true;
}

I'll leave you to do the checking for '0' etc - but hopefully you can see the benefits in structuring the "testing" part separately from the "getting input" part.


EDIT: Original answer...

Sounds like you want a regular expression:

Pattern pattern = Pattern.compile("[A-Z]{2}[1-9][0-9]{4}");

Full sample:

import java.util.regex.*;

public class Test {

    private static final Pattern PLATE_PATTERN =
        Pattern.compile("[A-Z]{2}[1-9][0-9]{4}");

    public static void main(String args[]) {
        checkPlate("AB10000");
        checkPlate("AB10000BBB");
        checkPlate("AB1CCC0BBB");
    }

    static void checkPlate(String plate) {
        boolean match = PLATE_PATTERN.matcher(plate).matches();
        System.out.println(plate + " correct? " + match);
    }
}

Of course, that doesn't tell you which bit was wrong. It also doesn't help you work out what was wrong with your original code... see earlier part.


Don't use a character based approach. Take the whole string and use the regex list above and either fail or pass it as a one time operation. You don't need that level of control here to get a pass/fail result.

HTH, James


You should really use a regular expression for this.

However, if you want to fix the problem with your code: the problem with your approach is that you are asking for input again after you've done some validation.

For example, if the first two characters are correct, the second loop will validate the input single-handedly. If that's wrong and it asks for input again, the user could input the first two characters incorrectly and that check won't be done because the code will have passed the first stage and is now only checking the second stage.

If you were to continue with your approach, you should make one large loop which would repeat if anything is wrong in the input and do all of the checks again in order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜