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] + " ");
}
}
}
}
} }
Comments
Leave a comment