2017-04-16 90 views
0

我有一個程序,從具有兩個字段,一週中的一天(1-7)和當天的溫度的輸入文件讀取。然後將它們輸入到數組(highArray,lowArray)中,並確定每天的高,低,總和平均溫度,並將它們寫入輸出文件。我遇到的問題是如何將我的數組的內容寫入我的輸出文件。我可以用這行代碼寫下星期幾:myOutput.writeInt(dow);我意識到Output類INT不適用於數組,因此如何寫入以將文件內容存檔到我的數組中? (highArray,lowArray)下面是我的代碼如何將數組內容寫入輸出文件?

package dow; 

import java.util.Arrays; 

public class DOW 
{ 
    public static void main(String[] args) 
    { 
     // INITIALIZATION 
     InputFile myInput = new InputFile("in.txt"); 
     OutputFile myOutput = new OutputFile("out.txt"); 

     int dow=0; 
     int temperature = 0; 
     int[] highArray = new int [8]; 
     int[] lowArray = new int [8]; 
     int[] countArray = new int [8]; 
     int[] totalArray = new int [8]; 

     // initialize array 
     for (dow = 0; dow <8; dow++) 
     { 
      totalArray [dow]= 0; 
      countArray [dow]= 0; 
      highArray [dow]= -999; 
      lowArray [dow]= 999;    
     } 

     while (!myInput.eof()) 
     { 
      dow = myInput.readInt(); 
      temperature = myInput.readInt(); 
      if (temperature > highArray[dow]) // High Per Day 
      { 
       highArray [dow] = temperature; 
      } 
      if (temperature < lowArray [dow]) // Low Per DAy 
      { 
       lowArray[dow] = temperature; 
      } 

      countArray [dow] = countArray[dow] +1; 
      totalArray[dow] = totalArray [dow] + temperature; 

      System.out.println(dow); 
     } 
     System.out.println(Arrays.toString(lowArray)); 
     System.out.println(Arrays.toString(highArray)); 
     System.out.println(Arrays.toString(totalArray)); 
     System.out.println(Arrays.toString(countArray));     

     //OUTPUT LOOP    

     for(dow = 1; dow < 8; dow++) 
     { 
      outputFile.println(highArray[dow]); 
     } 

     myOutput.writeInt(dow); 

     // myOutput.write(highArray);   
     // myOutput.writeInt(totalArray); 
     // myOutput.writeEOL(countArray); 
     // myOutput.writeInt (temperature); 

     myOutput.close(); 
    }  
} 
//END OF MAIN 
+0

這是不'javascript'相關的,因此我除去標籤。 'javascript!== java' – Ricky

+0

@Ricky它實際上是'!='而不是'!==':P –

+0

你想要寫入文件的數組內容是什麼? 'Arrays.toString(highArray)'格式不滿足您的需求嗎? –

回答

0
  • 我會強烈建議使用BufferedReaderBufferedWriter代替InputFileOutputFile

  • 作爲一個附註,將int數組中的所有值初始化爲0是微不足道的,因爲System.out.print(Arrays.toString(new int[10]));將打印出全0。 int是Java中的一種原始類型,因此它始終初始化爲0並且不能爲null。這也適用於數組。

實施例:

package dow; 

import java.util.Arrays; 

public class DOW 
{ 
    public static void main(String[] args) 
    { 
     BufferedReader reader = new BufferedReader(new FileReader("in.txt")); 
     BufferedWriter writer = new BufferedWriter(new FileWriter("out.txt")); 

     int dow = 0; 
     int temperature = 0; 
     int[] highArray = new int [8]; 
     int[] lowArray = new int [8]; 
     int[] countArray = new int [8]; 
     int[] totalArray = new int [8]; 

     // initialize array 
     for (dow = 0; dow <8; dow++) 
     { 
      highArray [dow]= -999; 
      lowArray [dow]= 999; 
     } 

     String nextLine; 
     while ((nextLine = reader.nextLine()) != null && !nextLine.isEmpty()) 
     { 
      //DO STUFF 
     } 
     reader.close(); 

     writer.write(Arrays.toString(highArray)); 

     writer.close(); 
    } 
} 
相關問題