I am supposed to create a file that will generate 100 random numbers and then sort them. I am stuck on the sorting. I think it would be a really simple solution but i cant seem to figure it out. I have tried the arrays utility and creating a method to sort but I keep getting a lot of zeros and i know that isn't right. I know something around line 32 isnt right. Any help would be greatly appreciated. Thanks!
import java.util.Random; //import random number generator
import java.io.*;
import java.util.Arrays;
public class NumGen {
public static void main (String [] args)throws IOException { //creates an exception
Random digit = new Random();//assigning the variable digit as a random number
int numbers[]= new int[100];//array with 100 numbers
File file = new File("Exercise8_19.txt");
if (file.exists()){
System.out.println("The file "+file.getName()+ " already exists.");
System.out.println();
}//ends if statement
else {
System.out.println("Your file has been written to " + (file.getName()+ "."));
System.out.println();
}
try {
Writer output = null;
File x = new File("Exercise8_19.txt");//name of txt file
output = new BufferedWriter(new FileWriter(x));
for (int i=0; i<numbers.length; i++){//for loop to create 100 random numbers
numbers[i] = (digit.nextInt (100));//set the array to the random numbers with a range of 0 - 100
Arrays.sort(numbers);
output.write(numbers[i]+" ");//output is the array of random numbers separated by a space
}//ends for loop
output.close();//ends output
FileInputStream fstream = new FileInputStream("Exercise8_19.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}//ends try
catch (IOException e) {// if there is no file
System.err.println("The file was not saved.");//error message
e.printStackTrace();
}//ends catch
}//ends main
}//ends class