I've been trying to figure out how to do this code, but can't figure out how to change my current code to work.
The problem reads:
Write a program to read an integer N (2 <= N <= 30) from an input filename “input5_01.txt” and then write a function to create an N x N matrix with random integers from the range [10, 99]. Output that matrix to the output filename “output5_01.txt”
Currently, here's my code:
public static final int UPPER = 99, LOWER = 10;
public static void main(String[] args) throws FileNotFoundException {
int n = 5;
int[][] arr = new int[n][n];
Scanner inputFile = new Scanner(new File("input5_01.txt"));
//read in an int (n) from input file
while (inputFile.hasNext()) {
if (inputFile.hasNextInt() >= 2 && inputFile.hasNextInt() <= 30) {
n = inputFile.nextInt();
} else {
inputFile.next();
}
}
//assigning random numbers to array
Random rand = new Random();
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[0].length; c++) {
arr[r][c] = rand.nextInt(UPPER - LOWER + 1) + LOWER;
}
System.out.println();
}
}
Really, the only problem I have is:
if(inputFile.hasNextInt() >= 2 && inputFile.hasNextInt() <= 30)
What can I do to make this work?