THINK FIRST·CODE LATER

← All labs

Masking a bank card number

Problem

Receipts never show a whole card number. Write CardMask: read one line containing a 16-digit card number that may be typed with spaces or dashes (6222-0212 3456 7890). Remove the separators, then print the number of digits, the number grouped by four, the masked form, the first six digits (the bank code), the first digit as a number, and whether it starts with 62.

For the input

6222-0212 3456 7890

the program prints

Digits: 16
Grouped: 6222 0212 3456 7890
Masked: **** **** **** 7890
Bank code: 622202
First digit: 6
Starts with 62: true

Use replace, substring, length, charAt and startsWith — no loops.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l5-6-data-1.txt

6222-0212 3456 7890
What your program must print for l5-6-data-1.txt
Digits: 16
Grouped: 6222 0212 3456 7890
Masked: **** **** **** 7890
Bank code: 622202
First digit: 6
Starts with 62: true

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l5-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l5-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.