Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用数组从输入文件读入整数并写入输出文件_Java_Arrays - Fatal编程技术网

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

在Java中使用数组从输入文件读入整数并写入输出文件,java,arrays,Java,Arrays,好的,所以我不完全确定我是否正确使用了数组,但接下来。我有一个家庭作业,我正在尝试使用数组从输入文件中读取并写入输出文件。我必须显示高温和低温,并找出一周中每天的平均值。我现在遇到的问题是输出文件中没有写入任何内容。 这是我到目前为止所拥有的 package dowtemps; import java.util.Arrays; public class DOWTemps { public static void main(String[] args) { in

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

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();
    }
}
package-dowtemps;
导入java.util.array;
公营道登
{
公共静态void main(字符串[]args)
{
int指数=0;
内部温度=0;
int道指=0;
int[]lowTempArray=新int[8];
int[]highTempArray=新int[8];
int[]countArray=新的int[8];
int[]totalArray=新的int[8];
InputFile in=新的InputFile(“input.txt”);
OutputFile out=新的OutputFile(“output.txt”);
System.out.println(“陶氏温度已启动,请稍候…”);
用于(索引=0;索引<8;索引++)
{
lowTempArray[索引]=999;
highTempArray[索引]=-999;
countArray[索引]=0;
totalArray[索引]=0;
陶氏++;
}
System.out.println(Arrays.toString(lowTempArray));
System.out.println(Arrays.toString(highTempArray));
而(!in.eof())
{
//读入记录
道琼斯指数=in.readInt();
temp=in.readInt();
//加载阵列
lowTempArray[dow]=温度;
highTempArray[dow]=温度;
如果(温度>高温度阵列[dow])
{
highTempArray[dow]=温度;
}
其他的
{
lowTempArray[dow]=温度;
}
totalArray[dow]=totalArray[dow]+温度;
countArray[dow]++;
//写记录
冲销(道指);
out.writeInt(低速数组[dow]);
out.writeInt(高速数组[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(“陶氏温度成功完成”);
out.close();
}
}

也许您可以为此更改输入文件和输出文件:

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();
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.PrintWriter;
导入java.io.UnsupportedEncodingException;
导入java.util.array;
导入java.util.Scanner;
公营道登
{
公共静态void main(字符串[]args)引发FileNotFoundException、UnsupportedEncodingException
{
int指数=0;
内部温度=0;
int道指=0;
int[]lowTempArray=新int[8];
int[]highTempArray=新int[8];
int[]countArray=新的int[8];
int[]totalArray=新的int[8];
扫描仪输入=新扫描仪(新文件(“input.txt”);
PrintWriter out=新的PrintWriter(“output.txt”、“UTF-8”);
System.out.println(“陶氏温度已启动,请稍候…”);
用于(索引=0;索引<8;索引++)
{
lowTempArray[索引]=999;
highTempArray[索引]=-999;
countArray[索引]=0;
totalArray[索引]=0;
陶氏++;
}
System.out.println(Arrays.toString(lowTempArray));
System.out.println(Arrays.toString(highTempArray));
while(在.hasNextLine()中)
{
//读入记录
道琼斯指数=in.nextInt();
temp=in.nextInt();
//加载阵列
lowTempArray[dow]=温度;
highTempArray[dow]=温度;
如果(温度>高温度阵列[dow])
{
highTempArray[dow]=温度;
}
其他的
{
lowTempArray[dow]=温度;
}
totalArray[dow]=totalArray[dow]+温度;
countArray[dow]++;
//写记录
印地安指数(道指),;
out.println(低温度阵列[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(“陶氏温度成功完成”);
out.close();
}
}

只要检查程序的逻辑,如果它不是您需要的

它看起来不像InputFile或OutputFile是标准Java对象。这些文件是由项目中的其他文件定义的吗?我怀疑您正在写入数据,但您没有在正确的位置查找该文件,也没有重新打开它。否则,如果类
OutputFile
中存在bug,您确定
循环正在删除吗?当您在调试器中单步执行代码时,会看到什么?我想我会检查我的逻辑。。。当我开始写的时候写的很好。。。所以也许这是我的逻辑。。。