Results 1 to 10 of 10
  1. #1
    javamann is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    javamann is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default 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();
    }
    }
    ====================

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Seaching a text file for credit card numbers

    Do you understand what p.matcher("a") does?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    javamann is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default 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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Searching a text file for credit card numbers

    Quote Originally Posted by javamann View Post
    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
    But you're passing it a literal String "a" and that certainly isn't a valid credit card number ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    javamann is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Seaching a text file for credit card numbers

    oops ... corrected and worked. Thanks.

  8. #8
    javamann is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default 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?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    javamann is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Searching a text file for credit card numbers

    fixed that and it worked. thanks much

Similar Threads

  1. Replies: 1
    Last Post: 09-30-2011, 12:13 AM
  2. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 10:11 AM
  3. Masking a credit card value
    By Samurai Coder in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 09:05 PM
  4. Credit Card Validator
    By bluegti02 in forum New To Java
    Replies: 2
    Last Post: 06-17-2008, 06:09 AM
  5. [SOLVED] File I/O Extra Credit Assignment FTW!
    By Bascotie in forum New To Java
    Replies: 9
    Last Post: 06-10-2008, 08:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •