0
Can there multiple scanners in one program? Or can they only be declared once
4 Answers
+ 1
There can be many. But they should wrap around different input streams.
0
No need multiple scanners for a single input stream. You can create one, and use it through out program for reading input.
see example here :
https://stackoverflow.com/questions/19766566/java-multiple-scanners
https://stackoverflow.com/questions/29053580/cannot-use-multiple-scanner-objects-in-java
https://stackoverflow.com/questions/65503259/should-a-scanner-only-be-instantiated-only-once-if-thats-the-case-why-so
0
Sololearn doesn't support two Scanners of System.in
but this exsmple works
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc1 = new Scanner("a b c d");
Scanner sc2 = new Scanner("1 2 3 4");
System.out.println( sc1.next() );
System.out.println( sc2.next() );
System.out.println( sc1.next() );
System.out.println( sc2.next() );
}
}