问题
So I have a simple Scanner object:
Scanner scan = new Scanner (System.in);
I am trying to use this Scanner object inside a method that I have declared in a different class. Is there anyway this can be done? I have also tried using:
public static final Scanner scan = new Scanner(System.in);
but this didn't really help
回答1:
public static final Scanner scan = new Scanner(System.in);
As you have declared Scanner
instance as public
and static
. So you access it using the Class reference.
Code in the other class.
String intpu = ClassName.scan.next();
ClassName
is the name of the class in which it is defined.
There could be other ways of achieving this:
Pass this to method of the other class where you want to use it.
If you define
Scanner
instance as member of the class, then create the instance of class and access it using the getter method.You can create a fresh instance of the
Scanner
in the other class.
回答2:
A common design pattern to address this is dependency injection. One form of dependency injection is called constructor injection. For this, create a constructor which accepts the object the class depends on. Then when you instantiate that class, you pass in the object you would like it to operate on (in your case your scanner).
Example
Main
- Caller class:
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
AnotherReader anotherReader = new AnotherReader(scan);
anotherReader.readSomething();
}
}
AnotherReader
- class which requires a Scanner
dependency:
public class AnotherReader {
Scanner scan;
public AnotherReader(Scanner scan)
this.scan = scan;
}
public void readSomething(){
String line = scan.nextLine();
// Do something
}
}
回答3:
You can pass the Scanner object's reference to that Method in the call.
something like method(Scanner sc)
回答4:
You could try inheritance
:
Super Class:
public class superclass
{
Scanner scan=new Scanner(System.in);
//scanner object declared globally
//constructor
//other methods etc.
}
Sub Class:
public class subclass extends superclass
{
//the scanner object can be inherited
}
Using inheritance,the subclass can inherit the scanner object in the super class. I hope this works.Please let me know if something goes wrong.
Can also be done without inheritance(without extending):
Simply create the object of superclass in the concerned subclass method as:
superclass ob=new superclass(); //created superclass object String takeIN= ob.scan.next(); /*refers to scanner object in superclass because scanner object is declared a field(global object) in superclass.*/
Following your example,if the scanner object in superclass is made static,you can simply use it like:
Scanner takeIn=superclass.scan.next();
回答5:
My way of solving this problem is to add a Scanner parameter to the method I am calling of the class that needs to use the Scanner, and then pass the scanner variable as an argument when calling the method.
My main method:
import java.util.Scanner;
public class ScannerTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
CalledClass cC = new CalledClass();
cC.testMethod(input);
}
}
CalledClass.java:
import java.util.Scanner;
public class CalledClass
{
public void testMethod(Scanner x)
{
System.out.println("Enter anything");
String myString = x.next();
System.out.println(myString);
}
}
It's not the prettiest thing, but it works.
回答6:
Here is how I do it:
Create MyScanner.java in a new package (in this case: mypackage)
package mypackage; import java.util.Scanner; public class MyScanner { static Scanner scan = new Scanner(System.in); public static String scan() { return scan.next(); } public static String scanLine() { return scan.nextLine(); } public static int scanInt() { return scan.nextInt(); } }
Use it anywhere by (static) importing all methods from MyScanner class of mypackage.
package yourpackage; import static mypackage.MyScanner.*; public class Main { public static void main(String[] args) { System.out.print("put an integer : "); int a = scanInt(); System.out.println("int a = " + a); } }
If you don't want to use static import, change import static mypackage.MyScanner.*;
in MyScanner.java to import mypackage.MyScanner;
. By doing this, you cannot call your scanner methods directly anymore, you need to specify the class containing the methods. For example: MyScanner.scanInt()
, MyScanner.scan()
.
来源:https://stackoverflow.com/questions/34474045/is-there-any-way-i-can-use-a-scanner-object-in-a-different-class