I am trying to have my program create another file that can be named by the user and print off random numbers that the user wants to be printed out. Unfortunately when I run my program, it works up until I reach the last "What filename do you want to use" in which the program states
"File not found java.io.FileNotFoundException: (The handle is invalid)"
I am wondering what I can do to have the program actually create the file and have the user choose their own name for the file.
import java.io.*;
import java.util.*;
public class chooseRandNum{
public static void main(String[] args){
Random rand = new Random();
Scanner key = new Scanner(System.in);
System.out.println("How many random numbers do you want? ");
int totalRand = key.nextInt();
System.out.println("What is the smallest random number? ");
int smallRand = key.nextInt();
System.out.println("What is the largest random number? ");
int largeRand = key.nextInt();
System.out.println("What filename do you want to use? ");
String fname = key.nextLine();
File outputFile = new File(fname);
PrintStream outputStream = null;
try{
outputStream = new PrintStream(outputFile);
}
catch (Exception e){
System.out.println("File not found " + e);
System.exit(1);
}
int n = rand.nextInt(largeRand - smallRand + 1);
for(int i = 0; i <= 5; i++){
for(int j = 0; j <= totalRand; j++){
outputStream.print(n + ",");
}
outputStream.println();
}
}
}