Card name validation and identification

Report a typo

Write a program that uses regular expressions to check whether the input card number is valid and identifies the card network name.

The input must consist only of numbers. Your program must take a string as an input and print the name of the card network as output, choosing from the set of names specified below, or the message: "Card number does not exist”.

Card networks: Visa, Mastercard, American Express.

  1. A Visa card starts with 4 and has the length of 16 digits.

  2. A MasterCard starts with the numbers from 51 to 55, or a range of numbers starting from 2221 to 2720. All have 16 digits.

  3. American Express card numbers start with 34 or 37 and have 15 digits.

Sample Input 1:

4235 2345 6543 1234 

Sample Output 1:

Visa
Write a program in Java 17
import java.util.*;

class BankCard {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String numbers = scn.nextLine();
String card = numbers.replaceAll("\\s", "");
String visaRegex = // put your code here
String masterCardRegex = // put your code here
String americanExpressRegex = // put your code here

if (card.matches(visaRegex)) {
System.out.println("Visa");
} else if (card.matches(masterCardRegex)) {
System.out.println("MasterCard");
} else if (card.matches(americanExpressRegex)) {
System.out.println("AmericanExpress");
} else {
System.out.println("Card number does not exist");
}
}
}
___

Create a free account to access the full topic