Question

In: Computer Science

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

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

please answer in java

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

import java.util.Scanner;

class Main {

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    String choice = "yes";
    //loops until the choice is exit
    while (!choice.equals("exit")) {
      //Ask user for postal code
      System.out.print("Enter postal code: ");
      String code = s.next();

      //Find length of code
      int len = code.length();

      if (len != 6) {
        System.out.println("Postal code wrong length, format L9L9L9");
      } else {
        int letter = 0;
        int digit = 0;
        for (int i = 0; i < len; i++) {
          //Check if char is letter
          if (!Character.isDigit(code.charAt(i))) {
            letter++;
          }
          //Check if char is digit
          if (Character.isDigit(code.charAt(i))) {
            digit++;
          }
        }

        //Check as per validation rules defined
        if (letter == 3 && digit == 3) {
          System.out.println("Postal code is valid!");
        } else if (letter < 3 && digit > 3) {
          System.out.println(
            "Postal code has digits where there are supposed to be letters"
          );
        } else if (letter > 3 && digit < 3) {
          System.out.println(
            "Postal code has letters where there are supposed to be digits"
          );
        }
      }
      System.out.print("Want to enter more postal code, yes/exit: ");
      choice = s.next();
    }
  }
}

Related Solutions

QUESTION: Write regular expressions after the ► to validate the following inputs: a) Product codes that...
QUESTION: Write regular expressions after the ► to validate the following inputs: a) Product codes that have one or more letters followed by one or more digits. ► b) Nonnegative integers between 1,000 and 9,999, including the comma. ► c) Canadian zip code containing upper case characters only. ► d) License plate numbers that have the form:          Letter          Digit          Letter          Hypen (-)          Digit          Letter          Digit ►
Please change this code to follow the rules. The program must not use global variables. In...
Please change this code to follow the rules. The program must not use global variables. In another words, it must use local variables and pass-by-value or pass-by-reference parameters. #include <iostream> #include <string> #include <algorithm> using namespace std; struct expense { string Desc; double exp; }; expense arr[100]; int c = 0; void menu(); //1. show all void showArray(){ if (c>0){ for(int i=0;i<c;i++){ cout<<"AMOUNT("<<arr[i].exp<<") DESC"<<arr[i].Desc<<")"<<endl; } }else{ cout<<"There is no expense entry available"; } menu(); } //2. spend void addArray(){ string...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
Simple code please thats easy to follow. C++ Write a program that can be used to...
Simple code please thats easy to follow. C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
[JAVA] You will write a program to validate passwords for users, making sure they meet the...
[JAVA] You will write a program to validate passwords for users, making sure they meet the following criteria: Rules: Passwords must be at least 8 characters long Passwords can only contain alpha numeric characters (no spaces or special characters) Passwords must contain at least 1 uppercase character Passwords must contain at least 1 lowercase character Passwords must contain at least 1 numeric character (0-9) Passwords cannot contain the word “password” A password that does not meet all of these rules...
ASSIGNMENT: Write a program to ask for the name and test score for 8 students, validate...
ASSIGNMENT: Write a program to ask for the name and test score for 8 students, validate that the test score is between 0.0 and 100.0 inclusive, and store the names and test scores in 2 parallel arrays. Then, calculate and display the average of the test scores. Finally, display all the students who have test scores above the average. Example Run #1: (bold type is what is entered by the user) Enter student name #1: George Enter the score for...
Write a program to validate parenthesis of any given equation by using the following criteria (you...
Write a program to validate parenthesis of any given equation by using the following criteria (you MUST use stacks); Number of open parenthesis “(“must be same as number of close parentheses “)” For example if I input “3 + 4 * (98+34*(34+8)*34*(3+x)” the program should display an error message, because number of open parenthesis “(“is 3 and number of closed “)” parenthesis is 2. Stack-Driver.cpp: #include "stack.h" #include "stack.cpp" #include<stdio.h> #include<stdlib.h> #include<time.h> int main() {    stack<int> my_stack(100);    srand(time(NULL));...
Create a Java application called ValidatePassword to validate a user’s password. Write a program that asks...
Create a Java application called ValidatePassword to validate a user’s password. Write a program that asks for a password, then asks again to confirm it. If the passwords don’t match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid. From that method, call a different method to validate the uppercase, lowercase, and digit requirements for a valid password. Your program should contain at least four methods in addition...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT