[英]How to read integers from one text file, sort them from least to greatest, and then write them to another text file
我試圖將 100 個隨機整數寫入一個文本文件,將它們從最小到最大排序,然后將這些數字排序后寫入一個單獨的文本文件。 我了解如何將數字寫入一個文件,以及如何對它們進行排序。 如果這兩個文件尚不存在,則必須由程序創建它們。 例如,如果我在其上寫入原始 100 個隨機整數的“Numbers.txt”不存在,程序將為我創建該文件,就像我試圖在其上寫入排序數字的文本文件一樣。 我正在努力理解如何將排序的數字從一個文件寫入另一個文件。
我試圖從最初存儲數字的整數數組中獲取相同的數字,並使用 Arrays.sort 命令對其進行排序,然后將該信息寫入我希望稱為“Sorted.txt”的單獨文件中. 我在那里遇到了一個問題,我得到了一個不兼容的類型錯誤,說明 void 不能轉換為 int,但不知道如何在邏輯上修復這個錯誤。
import java.util.*;
import java.io.*;
public class Numbers {
public static void main(String[] args) throws Exception {
//check if source file exists
File number = new File("Numbers.txt");
File sorted = new File("Sorted.txt");
if (!number.exists()) {
try ( // create the file
PrintWriter output = new PrintWriter(number);
) {
for (int i = 0; i <= 100; i++) {
output.print(((int)(Math.random() * 999) + 1) + " ");
}
}
}
try (
Scanner input = new Scanner(number);
) {
int[] numbers = new int[100];
for (int i = 0; i < 100; i++)
System.out.print(numbers[i] + " ");
System.out.println();
if (!sorted.exists()) {
try (
PrintWriter output = new PrintWriter(sorted)
) {
for (int i = 0; i < 100; i ++) {
output.print((int)Arrays.sort(numbers));
}
}
}
}
}
}
預期的結果是第一個文本文件顯示的是隨機創建時的數字,而第二個文本文件顯示的是排序后的數字。 截至目前,我可以獲得第一個文件以隨機順序顯示數字,但甚至無法創建第二個文本文件,更不用說在其上排序和寫入的數字了。
Arrays.sort 返回 void( 參見文檔)。
你可以做的是對數組進行排序。 Arrays.sort(numbers);
然后將結果寫入文件。
for (int i = 0; i < 100; i ++) {
output.print(numbers[i] + " ");
}
完整示例:
import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) throws Exception {
//check if source file exists
File number = new File("Numbers.txt");
File sorted = new File("Sorted.txt");
if (!number.exists()) {
try ( // create the file
PrintWriter output = new PrintWriter(number);
) {
for (int i = 0; i <= 100; i++) {
output.print(((int)(Math.random() * 999) + 1) + " ");
}
}
}
try (
Scanner input = new Scanner(number);
) {
int[] numbers = new int[100];
for (int i = 0; i < 100; i++) {
numbers[i] = input.nextInt();
}
if (!sorted.exists()) {
try (
PrintWriter output = new PrintWriter(sorted)
) {
Arrays.sort(numbers);
for (int i = 0; i < 100; i ++) {
output.print(numbers[i] + " ");
}
}
}
}
} }
我建議將寫入文件的代碼提取到方法中。 由於它只需要一個路徑和內容,因此可以對這兩個文件重復使用,這將使您的生活更輕松。
如果您包含出現錯誤的行號,這也非常有幫助,因為並不總是清楚拋出異常的位置。
從我從你的問題中了解到的問題是關於生成一些數字,將它們寫入一個文件,最后對它們進行排序並將它們寫入另一個文件。 我使用相同的方法編寫了一些代碼,但結構有所調整。 我希望它有幫助。
public static void main(String[] args) {
int[] randomData = new Random().ints(100).toArray();//just a short way to generate numbers. Use your own.
File numbers = writteArray("Numbers.txt", randomData);
Arrays.sort(randomData);
File sorted = writteArray("Sorted.txt", randomData);
}
public static File writteArray(String Path, int[] randomNumbers){
File numbers = new File(Path);
//if the file does not exist it will be created automatically
try(PrintWriter fileWritter = new PrintWriter(numbers)) {
//just an example of how to write them. Use your prefered format
//also you can use the append function to not lose the previous content
for (int e : randomNumbers)
fileWritter.printf(" %d", e);
fileWritter.println();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
return numbers;
}
作為編輯,如果您之后不需要這些文件,您可以使 writeArray() 函數返回 void。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.