/**
 *@author M Kelleher
 *@version 2
 *Outputs a random array into a selected file
 */
import java.util.*;
import java.io.*;
public class RndArr {
    /**
     *The array!
     */
    public int[][] randomnumber;

    /**
     *The length and width thereof
     */
    public int length, width;

    /**
     *Creates an array, then assigns a random number<br/>
     *for each location in the array.
     */
    public RndArr(int arrLen, int arrWidth){
	randomnumber = new int[arrWidth][arrLen];
	length=arrLen;
	width=arrWidth;
	//go through the array assigning a random value for each spot
        for (int i=0; i<length; i++){
	    for(int j=0; j<width; j++){
		randomnumber[i][j] = (int)(Math.random()*(255));
	    }
	}
	
	
    }

    /**
     *Returns the value of a location<br/>
     *in the array.
     */
    public int rndarr(int a, int b){
	return randomnumber[a][b];
    }

    /**
     *Prints the array into a string<br/>
     *using stringbuffer's toString()
     */
    public String printArray(){
	StringBuffer s1 = new StringBuffer();
	for(int i = 0; i<length; i++){
		for(int j=0; j<width; j++){
		    s1.append(rndarr(i,j) + " ");
		}
		s1.append("\n");
	}
	return s1.toString();
    }
    
    /**
     *Runs the show, prompts for a length and width
     *then outputs the array
     *NEXT UP: Outputting to a file
     */
    public static void main(String args[]){
	int arrLen, arrWidth;
	try{
	    int w, l;
	    BufferedReader input = new BufferedReader
		(new InputStreamReader(System.in));
	    System.out.println("Enter a width for your array: ");
	    w = Integer.parseInt(input.readLine());
	    System.out.println("Enter a length for your array: ");
	    l = Integer.parseInt(input.readLine());
	    RndArr r1 = new RndArr(w, l);
	    System.out.println(r1.printArray());
	}
	catch(IOException e){
	    System.err.println(e);
	}    
    }	
}
