Я пытаюсь создать программу, которая считывает целые числа, перечисляет их в порядке возрастания, выводит их в файл с именем sorted.txt, вычисляет среднее и отображает среднее и общее числа. Я смог исправить почти всю программу, за исключением той части, где я поместил файл в порядке возрастания.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
Scanner inputFile=null;
try {
inputFile = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e) {
System.out.println("FILE NOT FOUND");
return;
}
int n;
int index=1;
ArrayList<Integer>list = new ArrayList<>();
Collections.sort(list);
double total = 0, avg;
while (inputFile.hasNext()) {
n = inputFile.nextInt();
list.add(n);
total += n;
}
//Here is where I have trouble
//I am able to get it to run if I delete these lines up to...
{
for (int j = i + 1; j < count; j++) {
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
//These lines. The rest works fine.
PrintWriter pw = new PrintWriter(new File("sorted.txt"));
for(Integer i:list)
pw.println(i);
pw.close();
inputFile.close();
avg = total / list.size();
System.out.printf("The Total of " +index+" numbers in file %.2f\n",total);
System.out.printf("The Average of " +index+" numbers in file %.2f",avg);
}
}
Если бы кто мог помочь, был бы очень признателен! Спасибо за любой вклад! Я новичок в программировании и пытаюсь учить себя.