Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Comparing 2 lines from two separate TXT files

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Comparing 2 lines from two separate TXT files

    Hello everyone, with this silly question (which I feel I should know, but am having a complete brain meltdown), I need to compare a line of text from one TXT file to another line from another TXT file.

    In my assignment, my instructor wants us to create a program that stores account#, first and last names and a balance in a TXT file called "clients", which I did,
     

    100 Alan Jones 348.17
    300 Mary Smith 27.19
    500 Sam Sharp 0.00
    700 Suzy Green -14.22
    950 Bob Dylan 25550.00
    975 Judy Smith 254.32
    985 John Good -56.28
    990 Tim Graw 7585.15




    also we had to create a transaction TXT file that stores the account# and transaction amount,
     

    950 -126.35
    700 -25.52
    500 -65.32
    985 300.0
    990 21.21




    and the last thing is to compare the two TXT files using the account# as the key,
    this would not be a problem if I was able to modify the transaction TXT file to include the names,
    this way I can just compare each line in its entirely. However since I am not allow to include the names,

    I need a way to compare just the account#'s and add the transaction amount to the original balance.
    This is were my brain has left me and I am drawing a blank on how to accomplish this.

    I would put my code here for all to see, but since my brain is blank, I don't have any for this method.

    Any suggestions on how to complete this task.

    Thanks.

    --- Update ---

    Just had a thought on how to do this, but would like input if it is the best way or not.

    Make two arrayLists from both the trans.txt and clients.txt. This way you can compare cells.

    ie:
    If cell 0,0 in arraylistTrans = 950, now we can loop through the arraylistClients 0,0 cells, till a match is found, update the balance and store it in the arrayListNewMast.
    Once all transactions have been applied, do a for loop to rewrite the arrayListNewMast to a new TXT file call newMast.txt

  2. #2
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing 2 lines from two separate TXT files

    OK, I was able to create two multidimension arrays to represent both the trans.txt and clients.txt files.
            // creates the transArray
            int outerCount = 0;
            File file = new File("trans.txt");
            try {
                Scanner scan = new Scanner(file);
     
                while (scan.hasNext()) {
                    line = scan.nextLine();
                    transToken = line.split(" ");
                    for (int innerCount = 0; innerCount < 2; innerCount++) {
                        trans[outerCount][innerCount] = transToken[innerCount];
                    }
                    outerCount++;
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FileMatch.class.getName()).log(Level.SEVERE, null, ex);
            }
            //creates the clientsArray        
            outerCount = 0; // resets the outerCount variable
     
            File file2 = new File("clients.txt");
            try {
                Scanner scan2 = new Scanner(file2);
     
                while (scan2.hasNext()) {
                    line = scan2.nextLine();
                    clientToken = line.split(" ");
                    for (int innerCount = 0; innerCount < 4; innerCount++) {
                        client[outerCount][innerCount] = clientToken[innerCount];
                    }
                    outerCount++;
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FileMatch.class.getName()).log(Level.SEVERE, null, ex);
            }

    However, in my original code, I manually set the multidimensional parameters manually,
        String[][] trans = new String[5][2]; // need to change 5 to represent the line count of client.txt
        String[][] client = new String[9][4]; // need to change 9 to represent the line count of client.txt

    and everything works perfectly, now I want to set the first parameter (5 and 9 respectfully) to the length of the appropriate TXT file, this way when they grow so will the array.
    So I tried the following,

        int transRows;
        int clientRows;
        String[][] trans = new String[transRows][2]; // need to change 5 to represent the line count of client.txt
        String[][] client = new String[clientRows][4]; // need to change 9 to represent the line count of client.txt

            System.out.println("Start TransRow");
     
            transRows = 0;
            File file = new File("trans.txt");
     
            // program is hanging up on try statement
            System.out.println("Start TRY statement");
            try {
                Scanner scan = new Scanner(file);
     
            while (scan.hasNext()) {
            transRows++;
            }
            System.out.println("End TRY statement");            
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FileMatch.class.getName()).log(Level.SEVERE, null, ex);
            }
     
            System.out.println("Total rows = " + transRows);
            return transRows;

    but when I run this, the code hangs up on the TRY statement

     

    run:

    Enter request
    1 - Create a new account
    2 - Add Transaction
    3 - View all current balances
    4 - View all current credits
    5 - View all current debits
    6 - View all with zero balances
    7 - Apply transactions
    8 - Terminate program

    ? 7

    Applying new transactions
    Start TransRow
    Start TRY statement




    any suggestions on where I screwed up.

    --- Update ---

    Got it working, here is my code for the trans.txt, the client.txt is pretty much identical.
        public int TransRow() {
            transRows = 0;
            File file = new File("trans.txt");
            if (file.exists()) {
                try {
                    FileReader fr = new FileReader(file);
                    LineNumberReader lnr = new LineNumberReader(fr);
                    int lineNumber = 0;
     
                    while (lnr.readLine() != null) {
                        transRows++;
                    }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(FileMatch.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(FileMatch.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
     
            System.out.println("Number of rows in the TXT file " + transRows);
            return transRows;
        }

     

    run:

    Enter request
    1 - Create a new account
    2 - Add Transaction
    3 - View all current balances
    4 - View all current credits
    5 - View all current debits
    6 - View all with zero balances
    7 - Apply transactions
    8 - Terminate program

    ? 7

    Applying new transactions
    Number of rows in the TXT file 6
    Copied successfully.
    Transactions applied


    Enter request
    1 - Create a new account
    2 - Add Transaction
    3 - View all current balances
    4 - View all current credits
    5 - View all current debits
    6 - View all with zero balances
    7 - Apply transactions
    8 - Terminate program

    ? 8
    BUILD SUCCESSFUL (total time: 21 seconds)





    now to start the comparison which should be easy.

    I will update this post if I succeed,

    if anyone has a better way to accomplish any of this code, please let me know.

  3. #3
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing 2 lines from two separate TXT files

    OK, I am 95% complete but am stuck on this last item,

    Currently I have 2 arrays,
    clientArray which looks like this
    Acc# Name Balance
    001 Tim Graw 7585.15
    300 Mary Smith 27.19
    325 Suzy Green -14.22
    500 Sam Sharp 0.00
    565 Alan Jones 348.17
    687 John Good -56.28
    889 Bob Dylan 25550.0
    900 Ken Leduc 1250.25
    975 Judy Smith 254.32

    transArray which looks like this
    Acc# Transaction
    001 21.21
    325 -25.52
    500 -65.32
    565 0.83
    889 300.0
    975 250.00

    I am running the following code to compare the first set of numbers and if the are the same, then the balance should be added to the transaction
        public void newMastData() { 
            int count = 0;
            for (int j = 0; j < client.length; j++) {
                if(count == trans.length)
                    break;
     
                    if (client[j][0].equals((trans[count][0]))) {
                        double newBalance = Double.parseDouble(client[j][3]) + Double.parseDouble(trans[count][1]);
                        newMast[count][0] = client[j][0];
                        newMast[count][1] = client[j][1];
                        newMast[count][2] = client[j][2];
                        newMast[count][3] = String.valueOf(newBalance);
                        count++;
                    }
                    else{
                        newMast[j][0] = client[j][0];
                        newMast[j][1] = client[j][1];
                        newMast[j][2] = client[j][2];
                        newMast[j][3] = client[j][3];                
                    }                
     
     
            }        
     
            System.out.println("\nNew mast array");
            for(int i = 0; i < newMast.length; i++){
                System.out.println(newMast[i][0] + " " + newMast[i][1] + " " + newMast[i][2] + " " + newMast[i][3]);
            }
     
            System.out.println("Transactions applied\n");
     
            System.exit(0);
        }

    but the outcome is only partially correct
     

    run:

    Enter request
    1 - Create a new account
    2 - Add Transaction
    3 - View all current balances
    4 - View all current credits
    5 - View all current debits
    6 - View all with zero balances
    7 - Apply transactions
    8 - Terminate program

    ? 7

    Applying new transactions
    Copied successfully.

    New mast array
    001 Tim Graw 7606.36
    325 Suzy Green -39.74
    500 Sam Sharp -65.32
    565 Alan Jones 349.0
    889 Bob Dylan 25850.0
    975 Judy Smith 504.32
    null null null null
    900 Ken Leduc 1250.25
    null null null null
    Transactions applied

    BUILD SUCCESSFUL (total time: 2 seconds)




    I've been at this for quite some time today, so my brain is not working fully, can someone point out my error please.

  4. #4
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing 2 lines from two separate TXT files

    Done, all I had to do was make this change,

    newMast[j][0] = client[j][0];
    (do this change on all lines)

    simple fix, but now time to shut down, give the old brain a rest.

Similar Threads

  1. JDK and JRE is not recognized on separate account.
    By Ricky Buen in forum Java IDEs
    Replies: 9
    Last Post: July 15th, 2018, 08:12 PM
  2. Comparing two log files ignoring the lines
    By Rajitha in forum Java Theory & Questions
    Replies: 1
    Last Post: July 16th, 2012, 11:00 AM
  3. Reading lines in files with tabs
    By imsuperman05 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 14th, 2012, 12:05 PM
  4. separate strings
    By ridg18 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 21st, 2011, 06:22 AM
  5. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM