I have a program that uses multiple classes, I want the other classes to be able to access the same scanner that I have declared in the main class, I assume it would be done using some sort of get method, however I am unable to find any resources to help me.
Here are the Scanners that I have made in my main class:
Scanner in = new Scanner(System.in);
System.out.println("Enter a filename");
String filename = in.nextLine();
File InputFile = new File (filename);
Scanner reader = new Scanner(filename);
The reader Scanner is the one I want to be able to access across the other classes that make up the program, can anyone give me some guidance on how I can do this? Thanks a lot for any help!
Simply use public static final Scanner in = new Scanner(System.in);
in you main
class.
After that you can call it from anywhere by MainClassName.in
.
Also, be careful with arguments you pass to Scanner. I guess you wanted to put InputFile
into Scanner
, rather than filename
=)
File InputFile = new File (filename);
Scanner reader = new Scanner(filename);