[英]How To Write Array Contents To Output File?
I have a program that reads from a input file that has two fields, a day of the week (1-7) and the temperature for that day. 我有一个程序从一个具有两个字段的输入文件中读取,一个星期几(1-7)和该天的温度。 It then enters them into an array (highArray, lowArray)and decides the High, Low, total and Average temperatures for each day and writes them to a output file. 然后将它们输入一个数组(highArray,lowArray),并确定每天的最高,最低,总和平均温度,并将它们写入输出文件。 The problem I am having is how to write the contents of my Arrays to my output file. 我遇到的问题是如何将数组的内容写入输出文件。 I can write the day of the week just fine with the line: myOutput.writeInt(dow); 我可以用以下行写星期几:myOutput.writeInt(dow); I realize that the Output class INT doesn't work with Arrays, so how can I write to file the contents on my Arrays? 我意识到Output类INT不适用于Arrays,那么如何写内容到Arrays上呢? (highArray, lowArray) BELOW IS MY CODE (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
I would highly suggest using BufferedReader
and BufferedWriter
instead of InputFile
and OutputFile
. 我强烈建议使用BufferedReader
和BufferedWriter
而不是InputFile
和OutputFile
。
As a side note, initializing all values in an int
array to 0 is trivial because System.out.print(Arrays.toString(new int[10]));
附带说明一下,将int
数组中的所有值初始化为0都很简单,因为System.out.print(Arrays.toString(new int[10]));
will print out all 0's. 将打印出全0。 int
is a primitive type in Java so it is always initialized to 0 and cannot be null
. int
是Java中的原始类型,因此始终将其初始化为0且不能为null
。 This applies to arrays as well. 这也适用于数组。
Example: 例:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.