user14520459
user14520459

Reputation:

I'm stuck on this java code using strings and characters. I cant get past the first input

QUESTION: Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of L9L9L9 where:

L is a letter 9 is a digit Your program should continue accepting postal codes until the user enters the word “exit”.

Sample run (user input is shown in bold underline):

Enter a postal code: T2T-3X7

Postal code wrong length, format L9L9L9

Enter a postal code: T2T3AA

Postal code has letters where there are supposed to be digits

Enter a postal code: T2T358

Postal code has digits where there are supposed to be letters

Enter a postal code: T2T3A8

Postal code is valid!

Enter a postal code: exit

MY CODE:

// Standard import for the Scanner class
import java.util.*;
public class postalCode {
    public static void main (String [] args) {
        // Create a Scanner object attached to the keyboard
        Scanner input = new Scanner (System.in);
        System.out.print("Enter a postal code: ");
        String postalCode = input.nextLine();
    
        while (!postalCode.contains("exit")) {
        

            if (postalCode.length() > 6){
                System.out.println("Postal code wrong length, format L9L9L9");
            
            }
            if (postalCode.length() < 6){
                System.out.println("Postal code wrong length, format L9L9L9");
            
            }
            if (postalCode.length() == 6) {
                for (int i = 0; i < postalCode.length(); i++) {
                    if (Character.isDigit(postalCode.charAt(0))){
                        System.out.println("Postal code has digits where there are supposed to be letters");
                        break;
                    }
                    if (Character.isDigit(postalCode.charAt(2))){
                       System.out.println("Postal code has digits where there are supposed to be letters");
                        break;
                    }
                    if (Character.isDigit(postalCode.charAt(4))){
                        System.out.println("Postal code has digits where there are supposed to be letters");
                        break;
                    }
                    if (Character.isLetter(postalCode.charAt(1))){
                        System.out.println("Postal code has letters where there are supposed to be digits");
                        break;
                    }
                    if (Character.isLetter(postalCode.charAt(3))){
                        System.out.println("Postal code has letters where there are supposed to be digits");
                        break;
                    }
                    if (Character.isLetter(postalCode.charAt(5))){
                        System.out.println("Postal code has letters where there are supposed to be digits");
                        break;
                    }
                    else {
                        System.out.println("Postal code is Valid!");
                        break;
                    }

                }
            
            
            }

        }
    }
}

Upvotes: 0

Views: 194

Answers (2)

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

You have to update postalCode inside a loop. Then it's better to extract all different checks to the separate methods instead of printing same result multiple times.

public class Foo {

    public static void main(String... args) {
        Scanner scan = new Scanner(System.in);

        while (true) {
            System.out.print("Enter a postal code: ");
            String postalCode = scan.nextLine();

            if ("exit".equalsIgnoreCase(postalCode))
                break;

            if (postalCode.length() != 6)
                System.err.println("Postal code wrong length, format L9L9L9");
            else if (!hasDigits(postalCode))
                System.err.println("Postal code has digits where there are supposed to be letters");
            else if (!hasLetters(postalCode))
                System.err.println("Postal code has letters where there are supposed to be digits");
            else
                System.out.println("Postal code is Valid!");
        }
    }

    private static boolean hasDigits(String postalCode) {
        for (int i = 1; i < postalCode.length(); i += 2)
            if (!Character.isDigit(postalCode.charAt(i)))
                return false;
        return true;
    }

    private static boolean hasLetters(String postalCode) {
        for (int i = 0; i < postalCode.length(); i += 2)
            if (!Character.isLetter(postalCode.charAt(i)))
                return false;
        return true;
    }
}

Upvotes: 0

Himanshu Singh
Himanshu Singh

Reputation: 2165

After your third if{ } block you need to add postalCode = input.nextLine(); so that you can take the input from user next time. Because of this you are not able to proceed after first input.

while (!postalCode.contains("exit")) {
        

            if (postalCode.length() > 6){
                System.out.println("Postal code wrong length, format L9L9L9");
            
            }
            if (postalCode.length() < 6){
                System.out.println("Postal code wrong length, format L9L9L9");
            
            }
            if (postalCode.length() == 6) {
                for (int i = 0; i < postalCode.length(); i++) {
                    if (Character.isDigit(postalCode.charAt(0))){
                        System.out.println("Postal code has digits where there are supposed to be letters");
                        break;
                    }
                    if (Character.isDigit(postalCode.charAt(2))){
                       System.out.println("Postal code has digits where there are supposed to be letters");
                        break;
                    }
                    if (Character.isDigit(postalCode.charAt(4))){
                        System.out.println("Postal code has digits where there are supposed to be letters");
                        break;
                    }
                    if (Character.isLetter(postalCode.charAt(1))){
                        System.out.println("Postal code has letters where there are supposed to be digits");
                        break;
                    }
                    if (Character.isLetter(postalCode.charAt(3))){
                        System.out.println("Postal code has letters where there are supposed to be digits");
                        break;
                    }
                    if (Character.isLetter(postalCode.charAt(5))){
                        System.out.println("Postal code has letters where there are supposed to be digits");
                        break;
                    }
                    else {
                        System.out.println("Postal code is Valid!");
                        break;
                    }

                }
            
              postalCode = input.nextLine();
            }

        }

Upvotes: 1

Related Questions