Results 1 to 10 of 10
- 11-19-2011, 07:49 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Seaching a text file for credit card numbers
Hi All
I am trying to create a basic java program that will check for credit card numbers within a text file.
I have this code to read the file:
========================
import java.io.*;
class ReadingFile {
public static void main(String[] args){
try{
FileInputStream filestream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream xx = new DataInputStream(filestream);
BufferedReader yy = new BufferedReader(new InputStreamReader(xx));
String stringLine;
//Read File Line By Line
while ((stringLine = yy.readLine()) != null) {
// Print the content on the console
System.out.println (stringLine);
}
//Close the input stream
xx.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
===================================
On this link below, there is a code that uses an algorithm to validate if a credit card number is valid or not.
Anatomy of Credit Card Numbers
I am looking for tips on how I can search the text file for patterns that have a credit card format (say visa format which is 16 digits that start with 4 so 4xxx xxxx xxxx xxxx)and then pass them to the code that validates if they are valid credit card numbers or not.I am looking for something simple just as a proof of concept. It does not have to be a production-like program.
Thank you in advance.
- 11-19-2011, 08:35 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Seaching a text file for credit card numbers
A few remarks:
1) Get rid of that DataInputStream; you don't need it;
2) A regular expression "4\\d{3} \\d{4} \\d{4} \\d{4}" can find the credit card number patterns in a String.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-20-2011, 08:01 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Seaching a text file for credit card numbers
Thanks Jos for the comments. Here is my new code below. I can read the file but I expect when using these commands:
Pattern p = Pattern.compile("4\\d{3} \\d{4} \\d{4} \\d{4}");
Matcher matcher = p.matcher("a");
if (matcher.find()) { System.out.printf (matcher.group());}
and given that my file contains 4417 1234 5678 9113 and 4417123456789113 I expected that I will be able to detect the card number and print it but that is not working. I am not getting errors though.
import java.io.*;
import java.util.regex.*;
import java.util.*;
class CreditCard1 {
public static void main(String[] args){
CheckFile a = new CheckFile();
a.openFile("C:\\textfile.txt");
a.checker();
a.closeFile();
}
}
==================
import java.io.*;
import java.util.*;
import java.util.regex.*;
class CheckFile {
private Scanner x;
public void openFile (String path){
try {
x = new Scanner (new File(path)).useDelimiter("[.]");// use period as delimiter
// System.out.println (x.next());
}
catch (Exception e){
System.out.println("file does not exist");
}
}
public void checker () {
Pattern p = Pattern.compile("4\\d{3} \\d{4} \\d{4} \\d{4}");
try {
while (x.hasNext()){
String a = x.next();
System.out.printf ("%s", a);// this will print the text file and it worked fine
Matcher matcher = p.matcher("a");
if (matcher.find()) { System.out.printf (matcher.group());}
}
}
catch (Exception e){System.out.println("some error");}
}
public void closeFile (){
x.close();
}
}
====================
- 11-20-2011, 10:14 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Seaching a text file for credit card numbers
Do you understand what p.matcher("a") does?
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-20-2011, 04:56 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Searching a text file for credit card numbers
it checks whether a particular string matches the pattern. Our pattern is stored in p and we are checking in a for a match in a.
a is defined as String a = x.next();
do you see anything wrong in this? do you recommend another way for checking the file and returning the value that matches our pattern?
Thank you
- 11-20-2011, 04:59 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 11-20-2011, 08:24 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Seaching a text file for credit card numbers
oops ... corrected and worked. Thanks.
- 11-20-2011, 08:44 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Seaching a text file for credit card numbers
One last thing I am hoping to do is get all the numbers that match the pattern and pass them to another class to verify if they are valid. to do that I changed the type of the checker method to String and added return statement to return the matcher.group(). but that is giving a compiler error. here is the method:
public String checker () {
Pattern p = Pattern.compile("4\\d{3} \\d{4} \\d{4} \\d{4}");
try {
while (x.hasNext()){
String a = x.next();
Matcher matcher = p.matcher(a);
while (matcher.find()) {
System.out.println ("This is a pattern of a credit card number:" + matcher.group());}
}
System.out.println ("Reached end of file");
}
catch (Exception e){System.out.println("error checking for Visa card numbers");}
return matcher.group();
}
========
the error is:
cannot find symbol
symbol : variable matcher
location: class CheckFile
so why is it not recognizing the matcher now?
- 11-20-2011, 09:32 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Seaching a text file for credit card numbers
The scope of your matcher variable is in the body of your try block. Outside of that block there is no matcher variable. Better collect all matched, potentially valid credit card numbers in a List<String> and pass that to another 'checker' method.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-21-2011, 01:44 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
nextInt() for numbers, nextLine() for text. What if i want both?
By erik9920 in forum New To JavaReplies: 1Last Post: 09-30-2011, 01:13 AM -
Question Card Layout, Card Management
By lrichil in forum AWT / SwingReplies: 1Last Post: 04-22-2010, 11:11 AM -
Masking a credit card value
By Samurai Coder in forum New To JavaReplies: 2Last Post: 12-02-2009, 10:05 PM -
Credit Card Validator
By bluegti02 in forum New To JavaReplies: 2Last Post: 06-17-2008, 07:09 AM -
[SOLVED] File I/O Extra Credit Assignment FTW!
By Bascotie in forum New To JavaReplies: 9Last Post: 06-10-2008, 09:20 AM
Bookmarks