Q:

Fill in commandLine by passing a file to the scanner

I am working on a game for school that recuires a lot of input in the commandline. I am now working on the last part. Everytime I want to check if my code works, I need to fill in 20 inputs in the commandline. This can take a lot of time. 
In school my teacher told me there is a way to do this by using the scanner and a txt file. But he didn't tell me how to do this..
The idea is this:
In the commandline I wil get prompted to fill in this:
Number 1: ...
Number 2: ...

In the file I wil put what needs to be filled in (for example):
5
10

For the scanner I want to do something like this:
File file = new File("example.txt");
Scanner scanner = new Scanner(file);

So everytime I run the program the scanner wil automaticly fill in the input and I don't need to waste my time doing this.
If there is a more simple way to do this, please let me know, (I am using netbeans btw).

A:

Assuming that you were using Scanner to get input from the console and now you want to get the input from a file, try this code
File file = new File("example.txt");
try {
    Scanner scanner = new Scanner(file);
    while(scanner.hasNextInt()){
        int input = scanner.nextInt();
        // Do whatever you were doing with the input
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Use the full path to the file when you are initializing the file. For example 

C:/working_dir/example.txt

