2 Replies - 1587 Views - Last Post: 08 December 2010 - 10:47 AM Rate Topic: -----

#1 xaarinx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-September 10

.dat file sorting?

Posted 06 December 2010 - 07:41 PM

Hey guys i'm working on inserting database contents into a binary tree exercise for school.
We are supposed to sort the contents of the file by account IDs in ascending order.
I have no idea how to do this!
I thought maybe along the lines of an inorder traversal but I don't know how to even implement that for this type of program.
The rest I have already coded (just printing out the specified account).
Some guidance would be awesome on how to do this.

//--------------------------------------------------------------------
//
//  Laboratory 12, In-lab Exercise 3                     IndexDB.jshl
//
//  (Shell) Indexed accounts database program
//
//  The student is to complete all missing or incomplete method 
//     implementations for this class
//
//--------------------------------------------------------------------

// Builds a binary search tree index for the account records in the
// text file accounts.dat.

import java.io.*;
import java.util.StringTokenizer;

//--------------------------------------------------------------------
//
// Definition specifying the accounts database
//

class AccntRec
{
    // Data members
    int acctID;                             // Account identifier
    String firstName;                       // Name of account holder
    String lastName;
    double balance;                         // Account balance
    
} // class AccntRec

//--------------------------------------------------------------------
//
// Definition specifying the database index
//

class IndexEntry implements TreeElem
{
    // Data members
    int acctID;                             // (Key) Account identifier
    long recNum;                            // Record number

    public int key ( )
    { return acctID; }                      // Return key field

} // class IndexEntry

//--------------------------------------------------------------------

class IndexDB
{
    // Constants
    public static final long BYTES_PER_RECORD = 38;// Number of bytes used to store
                                                 //   each record in the accounts
                                                 //   database file

    public static void main (String args[ ]) throws IOException 
    {
        AccntRec acctRec = new AccntRec( );     // Account record
        BSTree index = new BSTree( );           // Database index
        IndexEntry entry = new IndexEntry( );   // Index entry
        StringTokenizer strTokens;              // For reading String tokens
        int searchID;                           // User input account ID
        long recNum;                            // Record number
        String str,                             // For reading a String
               name;
            
        // Need random access on the accounts database file; r = read only
        RandomAccessFile inFile =                       
            new RandomAccessFile("Accounts.dat", "r");
        

     

        
        // Also need a tokenized input stream on the accounts database file
        //  to efficiently read just part of it
        // Note: buffering alone improves I/O efficiency -- fewer disk accesses
        BufferedReader bReader = 
                       new BufferedReader(new FileReader("Accounts.dat"));
        StreamTokenizer fileTokens = new StreamTokenizer(bReader);
        
        // Also need tokenized input stream from keyboard
        BufferedReader reader = 
            new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer keybdTokens = new StreamTokenizer(reader); 
        
      




        // Iterate through the database records. For each record, read the
        // account ID and add the (account ID, record number) pair to the
        // index.

        recNum = 0;
        while ( fileTokens.nextToken( ) != fileTokens.TT_EOF )
        {
            entry = new IndexEntry( );
            entry.acctID = (int)fileTokens.nval;
            entry.recNum = recNum++;
            index.insert(entry);
            bReader.skip(BYTES_PER_RECORD - 5);       // skip to beginning of next record
                                                    //  subtract number of characters
                                                    //  read for acctID + 1
        }
        
        // Output the account IDs in ascending order.
       
        	





        // Read an account ID from the keyboard and output the
        // corresponding record.
        System.out.println( );
        System.out.print("Enter record number: ");
        keybdTokens.nextToken( );
        recNum = (long)keybdTokens.nval;
        inFile.seek(recNum * BYTES_PER_RECORD);

        str = inFile.readLine( );                          // Read the record

        if (str != null)
        {   
            // Need to tokenize the String read by readline( )
            strTokens = new StringTokenizer(str);

            name = strTokens.nextToken( );                  // first String token
            acctRec.acctID = Integer.parseInt(name);        // Convert String to an int

            acctRec.firstName = strTokens.nextToken( );     // 2nd String token - firstName
            
            acctRec.lastName = strTokens.nextToken( );      // 3rd String token - lastName
            
            name = strTokens.nextToken( );                  // 4th String token
            acctRec.balance = Double.parseDouble(name);     // Convert String to a double
            
            // Display the record.
            System.out.println(recNum + " : " + acctRec.acctID + " "
                               + acctRec.firstName + " " + acctRec.lastName + " "
                               + acctRec.balance);

        }








        
        // Close the file streams
        inFile.close( );
        bReader.close( );
        
    } // main
    
} // class IndexDB



Is This A Good Question/Topic? 0
  • +

Replies To: .dat file sorting?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: .dat file sorting?

Posted 06 December 2010 - 10:06 PM

This will only read one line

        str = inFile.readLine( );                          // Read the record

        if (str != null)
        {   
            // Need to tokenize the String read by readline( )
            strTokens = new StringTokenizer(str);

            name = strTokens.nextToken( );                  // first String token
            acctRec.acctID = Integer.parseInt(name);        // Convert String to an int

            acctRec.firstName = strTokens.nextToken( );     // 2nd String token - firstName
            
            acctRec.lastName = strTokens.nextToken( );      // 3rd String token - lastName
            
            name = strTokens.nextToken( );                  // 4th String token
            acctRec.balance = Double.parseDouble(name);     // Convert String to a double
            
            // Display the record.
            System.out.println(recNum + " : " + acctRec.acctID + " "
                               + acctRec.firstName + " " + acctRec.lastName + " "
                               + acctRec.balance);

        }


you need something like
        str = inFile.readLine( );                          // Read the record

        while (str != null)
        {   
            // Need to tokenize the String read by readline( )
            strTokens = new StringTokenizer(str);

            name = strTokens.nextToken( );                  // first String token
            acctRec.acctID = Integer.parseInt(name);        // Convert String to an int

            acctRec.firstName = strTokens.nextToken( );     // 2nd String token - firstName
            
            acctRec.lastName = strTokens.nextToken( );      // 3rd String token - lastName
            
            name = strTokens.nextToken( );                  // 4th String token
            acctRec.balance = Double.parseDouble(name);     // Convert String to a double
            
            // Display the record.
            System.out.println(recNum + " : " + acctRec.acctID + " "
                               + acctRec.firstName + " " + acctRec.lastName + " "
                               + acctRec.balance);

           str = inFile.readLine( );                          // Read the record
        }


Was This Post Helpful? 0
  • +
  • -

#3 xaarinx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-September 10

Re: .dat file sorting?

Posted 08 December 2010 - 10:47 AM

Thanks for the reply! Actually i forgot I had a writekeys method previously written for a BSTree class so calling that worked great. I would like to actually change this program to search by account ID instead of record number now.
I'm not sure how to modify it however.
Any suggestions?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1