Write a Java program which will validate credit card numbers. Forthis you need t
ID: 3609784 • Letter: W
Question
Write a Java program which will validate credit card numbers. Forthisyou need to create just one program rather than two classes as youdid in the last
assignment. The program should ask the user to enter a credit cardnumber which is
to be validated. A message should be displayed indicating whetherthe number is valid
or not. The valid credit card numbers are stored in a data filenamed validate.txt.
In the beginning of the program, the data file named validate.txtshould be read
with each valid number being stored in an array created with theArrayList class.
After the array is built with the numbers read from the file, aloop should be created
which will ask for a credit card number. The array will then besearched. If the
credit card number is found in the array, then a message isreported that the credit
card number is valid; otherwise it is reported as invalid. Continueto loop until the
user enters a credit card number of 000000 which should stop theprogram.
Sample Output:
123123 is an invalid number
144414 is a valid number
Hints:
The program must be written using the ArrayList class.
The array should be built by reading the data file validate.txtwhich will
contain the valid credit card numbers and entering those numbersinto the
array list.
After the data file has been read and loaded into the array, a loopshould be
created to ask the user to enter a credit card number to bevalidated. And a
message displayed as to whether it is valid or invalid.
The user should enter a sentinel value of 000000 to signal thathe/she is
finished.
Validate.txt
121565
547845
258945
325478
144414
875421
741256
625412
451374
851421
745419
214365
285473
654213
568124
474788
856544
234532
366596
Explanation / Answer
import java.io.*; import java.util.ArrayList; public class CreditCard{ public static void main(String[] args)throwsIOException{ ArrayList list = newArrayList(); BufferedReader rdr = newBufferedReader(new InputStreamReader(System.in)); try{ FileInputStream fstream = new FileInputStream("textfile.txt"); DataInputStream in = newDataInputStream(fstream); BufferedReader br = new BufferedReader(newInputStreamReader(in)); String strLine; while((strLine = br.readLine()) != null) list.add(strLine); in.close(); }catch(Exception e){System.err.println("Error: " + e.getMessage());} String cardnum; while(true){ System.out.print("credit card number: "); cardnum =rdr.readLine(); if(cardnum.equals("000000")) break; if(list.contains(cardnum)) System.out.println(cardnum + " is a valid number"); else System.out.println(cardnum + " is an invalid number"); } System.out.println(" Programterminated."); } } Sample run: credit card number: 121565 121565 is a valid number credit card number: 123456 123456 is an invalid number credit card number: 000000 Program terminated. Hope this helps a lot. Rate! Rate! Rate!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.