How to use the system Use multiple scanner objects on in?

What is the correct way to use multiple scanner objects in my program For example, I use a scanner to read a file, and then I use the scanner again to prompt the user for input according to the contents of the file Shows an excerpt of my code

....
Scanner f = new Scanner (@R_419_1073@); //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 (@R_419_1073@);
       String userInput = getUserInput.next();
       .....
   }
}
....

It doesn't seem to work I need to use userinput Close? What did I do wrong?

What I don't understand is that the first @ r_ 419_ 1073 @ just get the file name After that, why does it interfere with the second @ r_ 419_ 1073@. As for the input object, it is from file instead of @ R_ 419_ 1073 @ read

Solution

Using multiple scanners on the same stream is a potential problem Scanners can (and will) consume streams – this can (and will) cause unexpected side effects You'd better not do that

If the input is turned off, the input (but the string has no closing method) is turned off for everyone - it's not much fun for anyone

Edit: do not create multiple buffered wrappers on an InputStream

See also java code question... Scanner related?, Some methods are also discussed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>