从输入文件读取整数并使用Java中的数组写入输出文件

时间:2015-04-05 01:08:20

标签: java arrays

好的,所以我不完全确定我是否正确使用数组,但是这里有。我有一个家庭作业,我正在尝试从输入文件中读取并使用数组写入输出文件。我必须显示高温和低温,并找出一周中每天的平均值。我现在遇到的问题是没有任何内容写入输出文件。 这是我到目前为止所拥有的。

package dowtemps;

import java.util.Arrays;

public class DOWTemps
{
    public static void main(String[] args)
    {
        int index = 0;
        int temp = 0;
        int dow = 0;
        int[] lowTempArray = new int[8];
        int[] highTempArray = new int[8];
        int[] countArray = new int[8];
        int[] totalArray = new int[8];

        InputFile in = new InputFile("input.txt");
        OutputFile out = new OutputFile("output.txt");

        System.out.println("DOW Temperature Started. Please Wait....");

        for (index = 0; index < 8; index++)
        {
            lowTempArray[index] = 999;
            highTempArray[index] = -999;
            countArray[index] = 0;
            totalArray[index] = 0;
            dow++;
        }
        System.out.println(Arrays.toString(lowTempArray));
        System.out.println(Arrays.toString(highTempArray));

        while (!in.eof())
        {
            //read in records
            dow = in.readInt();
            temp = in.readInt();
            //load arrays
            lowTempArray[dow] = temp;
            highTempArray[dow] = temp;

            if (temp > highTempArray[dow])
            {
                highTempArray[dow] = temp;
            }
            else
            {
                lowTempArray[dow] = temp;
            }

            totalArray[dow] = totalArray[dow] + temp;
            countArray[dow]++;
            //write records

            out.writeInt(dow);
            out.writeInt(lowTempArray[dow]);
            out.writeInt(highTempArray[dow]);
            out.writeInt(totalArray[dow]);
            out.writeInt(countArray[dow]);
            out.writeInt((totalArray[dow] / (countArray[dow])));
            out.writeEOL();
        }
        System.out.println(Arrays.toString(lowTempArray));
        System.out.println(Arrays.toString(highTempArray));
        System.out.println("DOW Temperature Completed Successfully.");
        out.close();
    }
}

1 个答案:

答案 0 :(得分:0)

也许您可以为此更改InputFile和OutFile:

Scanner in = new Scanner(new File("input.txt"));
PrintWriter out = new PrintWriter("output.txt", "UTF-8");

我对你的代码做了一些改动,现在它已经阅读并打印了。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Scanner;

public class DOWTemps
{
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException
    {
        int index = 0;
        int temp = 0;
        int dow = 0;
        int[] lowTempArray = new int[8];
        int[] highTempArray = new int[8];
        int[] countArray = new int[8];
        int[] totalArray = new int[8];

        Scanner in = new Scanner(new File("input.txt"));
        PrintWriter out = new PrintWriter("output.txt", "UTF-8");
        System.out.println("DOW Temperature Started. Please Wait....");

        for (index = 0; index < 8; index++)
        {
            lowTempArray[index] = 999;
            highTempArray[index] = -999;
            countArray[index] = 0;
            totalArray[index] = 0;
            dow++;
        }
        System.out.println(Arrays.toString(lowTempArray));
        System.out.println(Arrays.toString(highTempArray));

        while (in.hasNextLine())
        {
            //read in records
            dow = in.nextInt();
            temp = in.nextInt();
            //load arrays
            lowTempArray[dow] = temp;
            highTempArray[dow] = temp;

            if (temp > highTempArray[dow])
            {
                highTempArray[dow] = temp;
            }
            else
            {
                lowTempArray[dow] = temp;
            }

            totalArray[dow] = totalArray[dow] + temp;
            countArray[dow]++;
            //write records

            out.println(dow);
            out.println(lowTempArray[dow]);
            out.println(highTempArray[dow]);
            out.println(totalArray[dow]);
            out.println(countArray[dow]);
            out.println((totalArray[dow] / (countArray[dow])));
            out.println();
        }
        System.out.println(Arrays.toString(lowTempArray));
        System.out.println(Arrays.toString(highTempArray));
        System.out.println("DOW Temperature Completed Successfully.");
        out.close();
    }
}

只需检查程序的逻辑是否符合您的要求