Results 1 to 9 of 9
  1. #1
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Need help reading and writing with textfiles

    Hello everyone!

    I am currently working on an assignment that reads and writes information from text files.

    The assignment wants me to develop an application that will perform the following:


    Two files accompany this assignment, employee.txt and transaction_file.txt

    The master employee.txt file has the following layout:
    Employee_ID_number, Last_Name, First_Name, Salary, Number_Dependents, Department,

    The transaction_file.txt has the following layout:

    1112, D
    4444, A, Smith, Jones, 45000, 2, Shipping
    6666, U, Jones
    8900, A, Hill, Bill, 65000, 0, Accounting

    A = Add a record
    U = Update a record
    D = Delete a record

    Your program should read the files. There is no user input. The program must use only one pass through the employee file and one pass through the transaction file in a staggered stair step fashion. In other words donít take the first transaction record and search all the way through the employee list, then take the second transaction record and search all through the employee list again, and so on.

    The program should generate the following transaction log.

    1112 D record not deleted, record does not exist
    4444 A record not added, record already exists
    6666 U record Updated.
    8900 A record Added
    The idea I have here is that the program should use nested while loops.

    The first loop will iterate through each transaction until all transactions are complete.

    The second loop iterates through each employee record to locate each place to perform the transaction.

    This will allow the program to work in a staggered stair step fashion.

    Now, the problem I am having is how can I loop through this in order to perform a stair step.

    I have tried in the code below using the hasNextLine method, but unfortunately, I cannot find a way to go back to a previous record.

    Notice in the if statement if my transRecID <= empRecID is true it performs the method. On the other hand, if it is false it will produce the next record for the empRecID and the next transRecID which is what I do not want. I want to keep the same empRecID so I can begin the loop cycle using the empRecID where I left off from and compare it to the transRecID.

    Is there a way to do this?

    Maybe I need another way because I am having no luck with this at all.

    is there a way I can read the line of text and store it in a variable to perform loop operations in a stair step manner?

    for instance:



    Java Code:
    package employeeupdateapp;
    import java.io.*;
    import java.util.Scanner;
    
    public class UpdateApp
    {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws FileNotFoundException
        {
            PrintWriter out = new PrintWriter("transactionLog.txt");
            FileReader readLine = new FileReader("transaction_file.txt");
            Scanner inTransactionRow = new Scanner(readLine);
            FileReader file = new FileReader("employee.txt");
            Scanner inEmpFile = new Scanner(file);
            
            while(inTransactionRow.hasNextLine())
             {
               String row = inTransactionRow.nextLine();// read transaction and store in row
               String transID = row.substring(0, 4);  // store transaction id in transID
    
               while(inEmpFile.hasNextLine())
               {
                   String empRow = inEmpFile.nextLine(); // stores line of text from empl file              
                   String empID = empRow.substring(0, 4); // stores the empID only
                  
                   int transRecID = Integer.parseInt(transID);
                   int empRecID = Integer.parseInt(empID);
                   
                   char trans = row.charAt(6);// get A,U, D
    
                       // if the record is found perform add, update, or delete
                       if(transRecID <= empRecID)
                       {         
                          //method to perform transaction update, add, or delete  
                          EmployeeLog performTransaction = new EmployeeLog();
                          performTransaction.updateTransLog(trans, row, transRecID, empRecID, out);
                          break; // return to outer loop
                       }     
               }
            }
            out.close();
        }            
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    I take it the employee.txt file is ordered by id ascending, similarly for the transactions one?

  3. #3
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Yes, that is correct.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    OK.
    First, you probably ought to use the split() command in String to break up the data in each row, but that's just a suggestion (ie, if the id was not a fixed number of characters).

    Anyway, I would vary your processing depending on whether the current transaction is an Add, Update or Delete. If it's an Add you want to know when the current record in emp is greater than the transaction id...that's when you'd insert that new one into the file.

    For an Update or a Delete you want to know if it's equal (so update or delete)...or greater than (which means the record is missing).

  5. #5
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Well, here is the whole program, this way you can play with it and see what I am trying to do.

    I am using netbeans for this program.
    Attached Files Attached Files

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    I can see what you're trying to do, from your initial description.
    I just gave you a pointer to how you should handle A,U,D in a single loop through the employee file. Which is what you asked isn't it?

    You don't need hasNextLine(), or go back to a previous line with the skeleton description I gave above...do you?

  7. #7
    computerbum is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Well I finally got it working and I was wondering if someone could give me some opinions and advice as to what they think about this program.

    Also, at the last minute my instructor is telling us to use blue jay, what is blue jay and how do I use it?

    Java Code:
    
    import java.io.*;
    import java.util.Scanner;
    
    public class UpdateApp
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            /**
             * Construct two PrintWriter objects to update employee.txt
             * and print log transaction log details
             *
             * @param updateEmp PrintWriter that updates employee data
             * @param log PrintWriter that prints log data
             */
            PrintWriter updateEmp = new PrintWriter("UpdateEmployee.txt");
            PrintWriter log = new PrintWriter("transactionLog.txt");
            /**
             * Construct a FileReader object to read the text files
             *
             * @param readTrans FileReader that reads from the transaction_file.txt
             * @param readEmp FileReader that reads from the employee.txt
             */
            FileReader readTrans = new FileReader("transaction_file.txt");
            FileReader readEmp = new FileReader("employee.txt");
            /**
             * Construct two scanner objects that reads the records from the files
             * @param inTransactionRow the scanner that scans the transaction data
             * @param inEmpFile the scanner that scans the employee data
             */
            Scanner inTransactionRow = new Scanner(readTrans);      
            Scanner inEmpFile = new Scanner(readEmp);
            /**
             * Variables to store records and employee ID's
             * @param tRow stores each line of the transaction_file
             * @param empRow stores each line of the employee file
             * @param TID stores the employee ID located in the transaction file
             * @param EID store the employee ID located in the employee file
             * @param code stores Add, Update, or Delete code from the transaction_file
             */
            String tRow = inTransactionRow.nextLine();                   
            int TID = Integer.parseInt(tRow.substring(0, 4));               
            int EID = 0;
            char code = tRow.charAt(6);
            String empRow = "";
            
        while(inEmpFile.hasNextLine())
        {       
            empRow = inEmpFile.nextLine();                                    // Get the first line of the employee.txt file     
            EID = Integer.parseInt(empRow.substring(0,4));                    // Get the first employee ID code
    
            if(EID > TID)                                                     // check if employee record is greater than transaction record
            {
                if((EID > TID) && code == 'A')                                // if transaction record is A and record does not exist then
                {
                    updateEmp.println(tRow);                                  // print transaction to employee.txt file
                    updateEmp.println(empRow);                                // print next employee record to employee.txt file
                    log.println(tRow.substring(0, 7) + " Record added");                      // print transaction details to log
                    if(inTransactionRow.hasNext())                            // if another file exists then
                    {
                    tRow = inTransactionRow.nextLine();                       // get next transaction record
                    TID = Integer.parseInt(tRow.substring(0, 4));             // get the transaction ID
                    code = tRow.charAt(6);                                    // get next transaction code
                    } 
                }
    
                if((EID > TID) && code == 'D')                                 // if transaction code is D and record does not exist then
                {
                   updateEmp.println(empRow);                                  // print to employee.txt file
                   log.println(tRow.substring(0, 7) + 
                   " Record not deleted, record already exists");              // print transaction details to log
                   tRow = inTransactionRow.nextLine();                         // get next transaction record
                   TID = Integer.parseInt(tRow.substring(0, 4));               // get the transaction ID
                   code = tRow.charAt(6);                                      // get next transaction code
                }
            }
    
            else if((EID == TID) && code == 'A')                                 // if record already exist and transaction code is A then
            {
                updateEmp.println(empRow);                                       // print to employee.txt file
                log.println(tRow.substring(0, 7) + 
                " Record not added, record already exists");                     // print transaction details to log
                tRow = inTransactionRow.nextLine();                              // get next transaction record
                TID = Integer.parseInt(tRow.substring(0, 4));                    // get the transaction ID
                code = tRow.charAt(6);                                           // get next transaction code
            }
            else if((EID == TID) && code == 'U')                                // if record exists and transaction code is U then
            {
                updateEmp.println(empRow);                                      // print to employee.txt file
                log.println(tRow.substring(0, 7) + " Record updated");                          // print transaction details to log
                tRow = inTransactionRow.nextLine();                             // get next transaction record
                TID = Integer.parseInt(tRow.substring(0, 4));                   // get the transaction ID
                code = tRow.charAt(6);
            }
            else if((EID == TID) && code == 'D')                                // Else if record exists and transaction code is D then
            {
                log.println(tRow.substring(0, 7) + " Record deleted");                          // print transaction details to log
                tRow = inTransactionRow.nextLine();                             // get next transaction record
                TID = Integer.parseInt(tRow.substring(0, 4));                   // get the transaction ID
            }
            else                                                                // otherwise just update the employee record with existing one
            {
                updateEmp.println(empRow);                                      // print to employee.txt file
            }
        }
              log.close();                                                      // close the transaction_log file
              updateEmp.close();                                                // close the updated employee file
        }
    }

  8. #8
    bryant1234 is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Re: Need help reading and writing with textfiles

    Can u repost this code so i can see all of it??

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Need help reading and writing with textfiles

    Quote Originally Posted by bryant1234 View Post
    Can u repost this code so i can see all of it??
    Why should they? This is a forum, not a coding service.

    If you have a question, feel free to start a thread about it -- they're free. Don't hijack another poster's thread, and don't wake dead threads.

    Closing this.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Reading and Writing to XML - Help Please!
    By JonnySnip3r in forum New To Java
    Replies: 4
    Last Post: 01-17-2010, 10:55 PM
  2. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 10:44 AM
  3. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM
  4. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

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