0

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();
        }   
    }
}
pczeus
  • 7,195
  • 4
  • 34
  • 50

2 Answers2

1

String fname = key.nextLine(); should be String fname = key.next();

There are other problems with the code but this solves the issue in the question.

From the JavaDoc:

nextLine()

/**
 * Advances this scanner past the current line and returns the input
 * that was skipped.
 *
 * This method returns the rest of the current line, excluding any line
 * separator at the end. The position is set to the beginning of the next
 * line.
 *
 * <p>Since this method continues to search through the input looking
 * for a line separator, it may buffer all of the input searching for
 * the line to skip if no line separators are present.
 *
 * @return the line that was skipped
 * @throws NoSuchElementException if no line was found
 * @throws IllegalStateException if this scanner is closed
 */

next()

/**
 * Finds and returns the next complete token from this scanner.
 * A complete token is preceded and followed by input that matches
 * the delimiter pattern. This method may block while waiting for input
 * to scan, even if a previous invocation of {@link #hasNext} returned
 * {@code true}.
 *
 * @return the next token
 * @throws NoSuchElementException if no more tokens are available
 * @throws IllegalStateException if this scanner is closed
 * @see java.util.Iterator
 */
Robert Bain
  • 7,287
  • 5
  • 30
  • 50
0

Right after these statements:

System.out.println("What is the largest random number? ");
int largeRand = key.nextInt();

Put this

key.nextLine(); // clear lingering new line

Then continue:


System.out.println("What filename do you want to use? ");
String fname = key.nextLine();

And check out the following on this site: Scanner usage

WJS
  • 22,083
  • 3
  • 14
  • 32