IntGenerator - try { file2 = new...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//Trinity Martin //IT 218 //Week 05 //Lab / // this program greates a file of 100 random numbers and puts them in ascending order import java.io.*; import java.util.*; i public class IntGenerator { public static void main(String[] args) { FileWriter file1 = null; FileReader file2 = null; F // Create a file Exercise8_5.txt try { file1 = new FileWriter("namedata.txt"); PrintWriter out = new PrintWriter(file1); // Generate 100 random integers between 1 and 1000 Random random = new Random(); for (int i=0; i<100; i++) { int x = random.nextInt(1000); out.print(x + " "); } out.close(); file1.close(); } catch (Exception e) { e.printStackTrace(); } // Read integers from the file Exercise8_5.txt int[] numbers = null;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: try { file2 = new FileReader("namedata.txt"); BufferedReader in = new BufferedReader(file2); String line = in.readLine(); // Parse the line into 100 tokens, each token is an integer String tokens = line.split(" "); // Convert to integers numbers = new int[tokens.length]; for (int i=0; i<numbers.length; i++) { numbers[i] = Integer.parseInt(tokens[i]); } // Sort all numbers Arrays.sort(numbers); in.close(); file2.close(); } catch (Exception e) { e.printStackTrace(); } // Print out all numbers for (int i=0; i<numbers.length; i++) { if ((i%10)==0) System.out.println(); System.out.print(numbers[i]+"\t"); } } }...
View Full Document

This note was uploaded on 07/26/2011 for the course IT 218 taught by Professor Accampo,e during the Spring '11 term at ITT Tech Pittsburgh.

Page1 / 2

IntGenerator - try { file2 = new...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online