0

I'm having a bit of trouble figuring this out. I'm supposed to read information from a Random Access File that was provided to me and then place the information into several arrays(firstName,lastName,age,gpa,computerClub). I was told what each type of information was written as, but not in what order. how can I read this information and place it into the appropriate arrays. This is what I have so far:

import java.io.*;//imports the IO tools for use in this program
import java.text.DecimalFormat;//imports the decimal format tool for use in this program
public class RandomAccessProject
{
   public static void main(String[] args)throws Exception
   {

      String[] firstName = new String[100];//creates an array of length 100 called firstName
      String[] lastName = new String[100];//creates an array of length 100 called lastName
      int[] age = new int[100];//creates an array of length 100 called age
      double[] gpa = new double[100];//creates an array of length 100 called gpa
      boolean[] computerClub = new boolean[100];//creates an array of length 100 called computerClub   

      int numElements = loadArrays(firstName,lastName,gpa,age,computerClub);//invokes the loadArrays method to fill the created arrays with values from the text file.
      printArrays(firstName,lastName,gpa,age,computerClub,numElements);


   }


   /*
   * loadArrays
   * @param String[] first ,String[] last,double[] grades, int[] age, boolean[] club.
   * @return int count
   */
  public static int loadArrays(String[] first, String[] last, double[] grades,int[] age, boolean[] club) throws Exception
  {
      RandomAccessFile inputFile = new RandomAccessFile("rand.dat","r");//creates a new File object
      int count = 0;//creates and initializes a variable for dermining which element of an array is interacted with
      int r = 0;

      while((r = inputFile.read()) != -1)
      {
          String firstName = inputFile.readUTF();//takes the next string and places it into the firstName variable
          String lastName = inputFile.readUTF();//takes the next string and places it into the lastName variable
          int old = inputFile.readInt();
          double gpa = inputFile.readDouble();//takes the next double and places it into the score variable
          boolean compClub = inputFile.readBoolean();
          first[count] = firstName;//sets the element corresponding to the current value of count in the first array to the firstName array
          last[count] = lastName;//sets the element corresponding to the current value of count in the last array to the lastName array
          age[count] = old;
          grades[count] = gpa;//sets the element corresponding to the current value of count in the first array to the score array
          club[count] = compClub;
          count++;//increment count by one

      }
      return count;
    }

This yields an EOFException,and I have no idea what else might work. Note: count and printArrays refer to another part of the program that will operate similarly to another program ive written once i get the information into arrays

Iccirrus
  • 11
  • 1
  • 2
  • 4

1 Answers1

0

Without knowing the format of the file I can't be more specific - but in general your problem is that you are doing an inputFile.read() each time around the loop and checking the return from that - but you then go on and read a load more stuff from the input file without checking there is more stuff to read and without doing anything with the data you read the first time.

If the line is laid out like a CSV file then your best way to do it will be to use readLine and read in the file as Strings one line at a time.

Then use split() on the string to split the data on the line by whatever the separator is.

Then read the elements from the array split() returns into the data arrays.

Note also that you are not checking the size of the data arrays, you are just filling them up and hoping you read less than 100 lines. That will fail for larger files.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • For this, It was specified that it would be no more than 100 elements. I have no idea how the file is laid out as its a .dat file,rather than text. Its just difficult to find solutions to something that isn't specified in the book, in class,or any of the presentations. – Iccirrus Feb 23 '14 at 01:47
  • Open the .dat file inside notepad or similar (even though it's not .txt it will still read it and if its text data display it). – Tim B Feb 23 '14 at 12:18