By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

Reading trailer from .dat

P: 1
Well, I have to edit a program which we previously used for reading a header file to be able to read a trailer from a .dat file. The .dat file consists of 5 entries in each record and I am assuming atleast 5 records(which I believe the amount of records should be taken from the trailer). Also I believe he said the SENTINEL value must be -1.

Our instructor gave us an extremely limited amount of information on how to attempt to do this and its not in our text book and google provided no clear information on the subject that I could gather.

So if possible, could you point me in the direction of a webpage with information on this topic?

If there are no pages with relevent information, could you explain to me how I would go about editing my code below?

I bolded and underlined all areas which I believe are relevant to my problem and I also noted in certain areas using (*// ).

I would appreciate any input. I have been racking my brain for the past few hours trying to get this to work. Anyway, thank you.

.dat File(the one I have been using for testing) =
4 5 4 9 3
3 5 7 1 6
8 6 5 3 4
4 2 5 3 8
6 1 7 9 6
5

(*//I believe since I need to read a trailer file, that the # of records would be last compared to the header being first)

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileInputStream;
  4.  
  5. public class TrailerProgram
  6. {   
  7.     public static void main (String[] args)
  8.     {
  9.         final int SENTINEL = -1;
  10.          int trailer=0,i=0;   
  11.         long n1,n2,n3,n4,n5,small;
  12.         Scanner scanInput = null;     // Create a Scanner called scanInput
  13.  
  14.  
  15.  
  16.         // Data File
  17.         try
  18.         {
  19.  
  20.            scanInput = new Scanner
  21.                        (new FileInputStream ("TrailerFile.dat"));
  22.  
  23.            headings();
  24.  
  25.  
  26.  
  27.           // reading trailer
  28.           trailer = scanInput.nextInt();     (*// I am not sure how to scanInput from last value rather than from the first )
  29.          System.out.println ("\n\n\n      " + "Program Name");
  30.          System.out.println ("      " + "TRAILER VALUE IS " + trailer);
  31.          System.out.println ("\n" + "SET #       SMALLEST VALUE     INPUT VALUES FROM THE DATA FILE");
  32.  
  33.            for(i=1;i<=trailer;i++)         (*// I am not sure what he wanted the sentinel value to do at -1. )
  34.       {
  35.  
  36.                 n1 = scanInput.nextLong();
  37.                 n2 = scanInput.nextLong();
  38.                 n3 = scanInput.nextLong();
  39.                 n4 = scanInput.nextLong();
  40.                 n5 = scanInput.nextLong();
  41.  
  42.  
  43.                 small = findSmallValue ( n1, n2,n3, n4, n5);
  44.                 System.out.printf("%3d.",i);
  45.                 printValues(n1,n2,n3,n4,n5,small);   
  46.             }       
  47.  
  48.  
  49.  
  50.  
  51.          }
  52.          catch (FileNotFoundException e)
  53.          {
  54.             System.out.println ("\n\n\n\n"
  55.                     + "****  The following data file was not found.  ****");
  56.             System.out.println
  57.                     ( "****  TrailerFile.dat                 ****");
  58.  
  59.          }
  60.  
  61.     }
  62.  
  63. // ===================== findSmallValue =====================
  64.  //determine the smallest value of the four longs returned .
  65.  
  66.      public static long findSmallValue (long n1, long n2, long n3, long n4, long n5)
  67.      {    
  68.             long small = 0;
  69.  
  70.             if ((n1 <= n2) && (n1 <= n3) && (n1 <= n4) && (n1 <= n5))
  71.                  small = n1;
  72.             else if  ((n2 <= n1) && (n2 <= n3) && (n2 <= n4) && (n2 <= n5))
  73.                   small = n2;
  74.             else if  ((n3 <= n1) && (n3 <= n2) && (n3 <= n4) && (n3 <= n5))
  75.                   small = n3;
  76.             else if  ((n4 <= n1) && (n4 <= n2) && (n4 <= n3) && (n4 <= n5))
  77.                   small = n4;
  78.             else 
  79.                   small = n5;
  80.  
  81.         return small;
  82.  
  83.      } 
  84.  
  85. // ===================== printValues =====================       
  86. // 
  87.  
  88.      public static void printValues (long n1, long n2, long n3, long n4, long n5, long small)
  89.      {    
  90.  
  91.  
  92.          System.out.printf("%15d %15d %7d %5d %5d %5d\n",small,n1,n2,n3,n4,n5);
  93.  
  94.      }
  95.  
  96. }
Nov 19 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.