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