[英]Reading data from file into array then outputting data into another file
[英]Reading input file into array, sorting it, then outputting it to a file
我是编程和Java的新手,但我需要做一个作业,其中必须创建一个程序,该程序打开并读取输入了12个整数的输入文本文件,并将文本文件的编号读取为整数我创建的数组,将数组作为参数传递给将数组从低到高排序的方法,然后将这些排序后的数组编号写入输出文件。 输出文件还应显示所有整数的平均值,这些平均值是使用循环计算的,并位于整数排序列表的末尾。
以下是到目前为止的内容。 我似乎无法弄清楚如何正确地对数组进行排序并发送回主函数。 我也不清楚如何获得和输出平均值。 如果有人可以提供帮助,我将不胜感激。 先感谢您。
import java.util.Scanner;
import java.util.Arrays;
public class NumberSorter {
public static void main(String[] args) throws Exception {
double sum = 0;
double avg = 0;
double total = 0;
int i = 0,
number = 0;
int[] data_array = new int[12];
java.io.File file = new java.io.File("numbers.txt");
Scanner input = new Scanner(file);
while(input.hasNext()){
data_array[i] = input.nextInt();
sortArray(data_array);
avg = sum/total;
java.io.PrintWriter output = new java.io.PrintWriter("dataout.txt");
output.close();
}
}
public static void sortArray(int[] data_array)
{
Arrays.sort(data_array);
}
}
读入data_array
主要问题是每次您都读入位置0,因为您从不会在while
循环中增加i
的值。 因此,每次您用文本文件中的下一个值覆盖数组的第一个元素。
只需添加i++;
即可解决i++;
在data_array[i] = input.nextInt();
之下data_array[i] = input.nextInt();
然后,我建议在此循环外部进行输出和排序,将所有关注点分离 ( 注意:理想情况下,根据问题,所有操作都使用不同的方法或类完成,但是我将其留在这里的main
方法中,例如目的)。
因此,最好将sortArray
调用移到while
循环之外,因为当前这将对array
排序,然后尝试将下一个int
添加到下一个位置,但是由于array
现在处于不同的顺序( 可能是 ) ,它不会将其添加到您认为合适的位置。
您遇到的另一个问题是,您没有向dataout
文件写入任何内容。
写入文件的方法有很多,但这只是一个示例。
java.io.FileWriter fr = new java.io.FileWriter("dataout.txt");
BufferedWriter br = new BufferedWriter(fr);
try (PrintWriter output = new PrintWriter(br)) {
for (int j = 0; j < data_array.length; j++) {
System.out.println(data_array[j]);
output.write(data_array[j] + "\r\n");
}
}
然后,您可以计算平均值,并将其附加到文件末尾。
但是首先,您需要计算数组中所有数字的总和。
因此,与其创建另一个循环,不如将其添加到您的更早的while
循环中,并在每次迭代中添加该值。
sum += data_array[i];
当您使用array
(即固定长度)时,可以使用array.length()
来获取total
变量的值,或者只添加total++;
进入while
循环。
然后,您的avg = sum / total;
将工作。
完整代码:
public class NumberSorter {
public static void main(String[] args) throws Exception {
double sum = 0;
double avg = 0;
double total = 0;
int i = 0;
int[] data_array = new int[12];
java.io.File file = new java.io.File("numbers.txt");
Scanner input = new Scanner(file);
while (input.hasNext()) {
data_array[i] = input.nextInt();
//add to the sum variable to get the total value of all the numbers
sum += data_array[i];
total++;
//increment the position of 'i' each time
i++;
}
//only sort the array after you have all the elements
sortArray(data_array);
//gets the average of all elements of the array
avg = sum / total;
java.io.FileWriter fr = new java.io.FileWriter("dataout.txt");
BufferedWriter br = new BufferedWriter(fr);
try (PrintWriter output = new PrintWriter(br)) {
for (int j = 0; j < data_array.length; j++) {
//write each element plus a new line
output.write(data_array[j] + "\r\n");
}
//write the average (to two decimal places - plus it doesn't allow
//you to write doubles directly anyway) to the file
output.write(String.format("%.2f", avg));
output.close();
}
}
public static void sortArray(int[] data_array) {
Arrays.sort(data_array);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.