2014-02-23 26 views
0

我在解決這個問題時遇到了一些麻煩。我應該從提供給我的隨機訪問文件中讀取信息,然後將這些信息放入幾個數組中(名字,姓氏,年齡,gpa,computerClub)。我被告知什麼類型的信息被寫入,但不是以什麼順序。我如何讀取這些信息並將其放入適當的數組中。這是我到目前爲止:我將如何從隨機訪問文件中獲取信息並將其放入多個數組中?

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; 
    } 

這會產生EOFException,我不知道還有什麼可以工作。 注意:count和printArrays指的是程序的另一部分,其操作與編寫另一個程序類似,一旦我將信息讀入陣列中

回答

0

不知道文件的格式我不能更具體 - 但是一般來說你的問題是你每次都在循環中執行inputFile.read()並檢查返回值 - 但是你繼續閱讀並從輸入文件中讀取更多的東西,而不檢查是否有更多的東西要讀取和對第一次閱讀的數據不做任何處理。

如果將行按照CSV文件進行佈局,那麼最好的方法是使用readLine並將文件作爲字符串一次讀入一行。

然後在字符串上使用split()來分隔線上的數據,無論分隔符是什麼。

然後讀取數組中的元素split()返回數據數組。

還要注意的是,您並未檢查數據數組的大小,只是將它們填滿並希望讀取少於100行。對於較大的文件,這將失敗。

+0

爲此,已指定它不會超過100個元素。 我不知道如何將文件作爲一個.dat文件,而不是文本佈局。 對於本書,課堂或任何演示文稿中未指定的內容,它只是很難找到解決方案。 – Iccirrus

+0

打開記事本或類似文件中的.dat文件(即使它不是.txt,它仍會讀取它,如果它的文本數據顯示它)。 –

相關問題