2

私はこれを正しく行う方法に少し困惑しています。私はそれにいくつかの方法を取り入れました、そして今私は腰が深いです。だから私がやりたいのは、このように各行に2番目の束があるテキストファイルを解析することです。

1 10
2 12 
3 13 

等....

それぞれが1つのスペースで分割されます。オブジェクト内の変数に正しい番号を割り当てることさえできました。私のことは、配列を上書きし続け、残りの部分にテキストファイルのデータを入力しないことです。配列を印刷する場合は、基本的にテキストファイルを印刷する必要があると思います。

public static void main(String[] args) {
      
    Process [] pArray;
    //just need to have 10 Process
    pArray = new Process [10];
    
    //delimiter to parse string
    String delimiter = " ";
    String[] tokens;
    tokens = new String [10];
    
    /*
     * save this for input handeling
    Scanner input = new Scanner( System.in );
    System.out.println("Enter the Text file for data set");
    String fileDestination = input.next();
    * */
    
    //get data from the file
    File file = new File("C:/Users/Kenshin/Desktop/TestData.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    try {
  
        fis = new FileInputStream(file);     
        // Here BufferedInputStream is added for fast reading.     
        bis = new BufferedInputStream(fis);   
        dis = new DataInputStream(bis);    
        // dis.available() returns 0 if the file does not have more lines.    
        while (dis.available() != 0) {   
            // this statement reads the line from the file and print it to       
            // the console.        
            int g = 0;
            //System.out.println(dis.readLine());
            tokens = dis.readLine().split(delimiter);
            int aInt = Integer.parseInt(tokens[0]);
            int bInt = Integer.parseInt(tokens[1]);
            for( int i = 0; i < tokens.length; i ++)
            {
                
                //int aInt = Integer.parseInt(tokens[i]);
                pArray[g] = new Process(aInt, bInt);
                
            }
          
            g++;
            
        }
  
        // dispose all the resources after using them.     
        fis.close();     
        bis.close();
        dis.close();
    } catch (FileNotFoundException e) {  
        e.printStackTrace();
    } catch (IOException e) {  
        e.printStackTrace();
    }
    
    for(int i = 0; i < pArray.length; i ++)
    {
        System.out.print(pArray[i].arrivalTime + " ");
        System.out.println(pArray[i].burstTime);
    }
}
4

3 に答える 3

5

あなたはこれをする必要はありません

int g = 0;

あなたのループの外?それ以外の場合は、配列内の初期値を継続的に書き換えており、値を進めていません(配列の残りの部分にデータを入力しています)。

これを簡単にするためにArrayList<Process>、固定長配列ではなく、または他の同様のコレクションにデータを入力します。

于 2012-10-09T13:23:57.447 に答える
2

2つの問題があります。1つは、毎回再起動する必要がないことですg。次に、内側のforループは何もしません(使用している行iはコメントアウトされています)。次のことを試してください。

int g = 0;
while (dis.available() != 0) {   
    tokens = dis.readLine().split(delimiter);
    int aInt = Integer.parseInt(tokens[0]);
    int bInt = Integer.parseInt(tokens[1]);
    pArray[g++] = new Process(aInt, bInt);
}
于 2012-10-09T13:24:06.630 に答える
0

まずg、whileループのすべての反復で再初期化しています。次にg、forループ内でインクリメントするか、別の変数をのインデックスとして使用する必要がありますpArray。それ以外の場合は、forループの各反復でインデックス0の値を上書きし続けます。

int g = 0;
while (dis.available() != 0) {
    // this statement reads the line from the file and print it to
    // the console.

    //System.out.println(dis.readLine());
    tokens = dis.readLine().split(delimiter);
    int aInt = Integer.parseInt(tokens[0]);
    int bInt = Integer.parseInt(tokens[1]);
    for( int i = 0; i < tokens.length; i ++) {
        //int aInt = Integer.parseInt(tokens[i]);
        pArray[g] = new Process(aInt, bInt);
        g++;
    }
}
于 2012-10-09T13:33:23.510 に答える