Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple Scanner objects on System.in?

Tags:

what's the correct way to use multiple Scanner objects in my program. For example, I use scanner to read a file, then depending on what is found in the file, i use scanner again to prompt for user input. An extract of my code is shown

....
Scanner f = new Scanner (System.in); //get the file name
String fileName = f.next();
Scanner input = new Scanner( new File( fileName ) );
while ( input.hasNext() )
{
   String currentLine = input.nextLine();
   if ( some pattern found) {
       Scanner getUserInput = new Scanner (System.in);
       String userInput = getUserInput.next();
       .....
   }
}
....

It doesn't seem to work. Do I need to use userInput.close() ? What am i doing wrong. ?

What I don't understand is, the first System.in is just getting the file name. After that, why does it interfere with the second System.in. As for the input object, its reading from a File and not from System.in.