/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package randomtestfile; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Random; import java.util.Scanner; /** * * @author jcmtiernan */ public class RandomTestFile { /** * @param args the command line arguments * @throws java.io.IOException */ public static void main(String[] args) throws IOException { final int ROWMAX = 10; final int COLMAX = 30; int[][] array2DTest = new int[ROWMAX][COLMAX]; // this code generates a file of random numbers that can then be used // to repeat tests for other programs File fileIn; File fileIn2; File fileIn3; File gradePairFileIn; Scanner input; fileIn = fileFill(300); fileIn2 = fileFill2(300,100000); fileIn3 = fileFill3(300,500,"madeRandom.txt"); gradePairFileIn = gradeFileFill(30,160,110,"gradePairRandom.txt"); try { input = new Scanner(fileIn); } catch (FileNotFoundException fnf) { System.out.println("No file found"); input = new Scanner(System.in); } /* for( int r = 0; r < ROWMAX; r++) for(int c = 0; c < COLMAX; c++) { if (input.hasNextInt()) { array2DTest[r][c] = input.nextInt(); } } */ // Verifying data from file //printFileContents("randomOut.txt"); //printFileContents("randomOut2.txt"); //printFileContents("madeRandom.txt"); printFileContents("gradePairRandom.txt"); /* try { input = new Scanner(fileIn); } catch (FileNotFoundException fnf) { System.out.println("No file randomOut.txt found"); input = new Scanner(System.in); } int count = 0; System.out.println("File randomOut.txt contains:"); while (input.hasNextInt()) { System.out.printf("%8d",input.nextInt()); if (count++ % 10 == 9) { System.out.println(); } } */ System.out.println(); System.out.println(); /* System.out.println("array2DTest contains:"); for( int r = 0; r < ROWMAX; r++) { System.out.println("Elements on row "+r+" are: "); for(int c = 0; c < COLMAX; c++) { System.out.printf("%8d",array2DTest[r][c]); if (c % 10 == 9) { System.out.println(); } } System.out.println(); } */ } /** * This method creates a file of random numbers called randomOut.txt * between the values of 0 - 9999 inclusive * * @param count is the count of random numbers to generate and store in the file * @return File of random numbers * @throws IOException */ public static File fileFill(int count) throws IOException { File file = new File("randomOut.txt"); FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); // data will be put into the file in rows of 10 elements with a // total of ROWMAX * COLMAX values Random rand = new Random(); int randomNumber; for(int k = 0; k < count; k++) { randomNumber = rand.nextInt(10000); // 0-9999. bw.write(String.format("%8d", randomNumber)); if ((k % 10) == 9) { bw.newLine(); } } bw.close();// be sure to close BufferedWriter return file; } /** * This method creates a file of random numbers called randomOut2.txt * between the values of 0 - (randomRange - 1) inclusive * * @param count is the count of random numbers to generate and store in the file * @param randomRange is the value to bound the generated random numbers * @return File of random numbers * @throws IOException */ public static File fileFill2(int count, int randomRange) throws IOException { File file = new File("randomOut2.txt"); FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); // data will be put into the file in rows of 10 elements with a // total of ROWMAX * COLMAX values Random rand = new Random(); int randomNumber; for(int k = 0; k < count; k++) { randomNumber = rand.nextInt(randomRange); // 0 to randomRange - 1. bw.write(String.format("%8d", randomNumber)); if ((k % 10) == 9) { bw.newLine(); } } bw.close();// be sure to close BufferedWriter return file; } /** * This method creates a file of random numbers named by the user * between the values of 0 - (randomRange - 1) inclusive * * @param count is the count of random numbers to generate and store in the file * @param randomRange is the value to bound the generated random numbers * @param fileName is the name the user passes in for the new file * @return File of random numbers * @throws IOException */ public static File fileFill3(int count, int randomRange, String fileName) throws IOException { File file = new File(fileName); FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); // data will be put into the file in rows of 10 elements with a // total of ROWMAX * COLMAX values Random rand = new Random(); int randomNumber; for(int k = 0; k < count; k++) { randomNumber = rand.nextInt(randomRange); // 0 to randomRange - 1. bw.write(String.format("%8d", randomNumber)); if ((k % 10) == 9) { bw.newLine(); } } bw.close();// be sure to close BufferedWriter return file; } public static File gradeFileFill(int maxCount,int maxStdt, int maxValue,String fileName) throws IOException { File file = new File(fileName); FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); // data will be put into the file in rows of up to 30 grades and indexes per row Random rand = new Random(); int randomIndex; int randomGrade; int randomNumPairsOnLine; for(int n = 0; n