Hi all...I have this program that I wrote that creates a random number, writes it to a file and scans it back to the system. That part alone took me forever to hash out. Now I stuck. I would like to create 100 random integers and sort them in both the file and the printout.
Here is the code
import java.util.Random; import java.util.Scanner; public class ReadWriteData { // Main method public static void main (String[] args)throws Exception { //Create a file instance java.io.File file = new java.io.File("100 random sorted numbers txt."); if (file.exists()) { System.out.println("File Already Exists"); System.exit(0); } //Create a file java.io.PrintWriter output = new java.io.PrintWriter(file); //Create ramdom number to be sorted Random randomGenerator = new Random(); //Write formatted output to the file output.println(randomGenerator.nextInt(100)); //close the output file output.close(); //Create a Scanner for the file Scanner input = new Scanner(file); //Read data from a file while (input.hasNext()) { int score = input.nextInt(); System.out.println( score); } //Close the input file input.close(); } }
Could someone advise me on the logic and code placement needed to pull this off?