Java 使用 Scanner 作为构造函数的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13166971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java using Scanner as a parameter for a constructor
提问by user1781671
This is a question for a school assignment, which is why I am doing it in such a way.
这是一个学校作业的问题,这就是我这样做的原因。
Anyways, I make a scanner using Stdin in the main method (Scanner stdin = new Scanner(System.in); is the line), reading data from a txt specified when the program is run. This Scanner works as expected in the main, however I need to use it in an custom class that has Scanner as an argument:
无论如何,我在 main 方法中使用 Stdin 制作了一个扫描仪(Scanner stdin = new Scanner(System.in); 是行),从程序运行时指定的 txt 中读取数据。这个 Scanner 在 main 中按预期工作,但是我需要在一个以 Scanner 作为参数的自定义类中使用它:
public PhDCandidate(Scanner stdin)
{
name = stdin.nextLine();
System.out.println(name); //THIS NEVER RUNS
preliminaryExams = new Exam[getNumberOfExams()];
for(int i = 0; i <= getNumberOfExams(); i++)
{
preliminaryExams[i] = new Exam(stdin.nextLine(), stdin.nextDouble());
}
System.out.print("alfkj");
}
At this point any call of the Scanner will just end the program, with no exceptions or errors thrown. Only calling .next() works. I could make the program work, but it would be hacky, and I really don't understand what is happening. I suspect I am missing a very simple concept, but I'm lost. Any help would be appreciated.
此时,对 Scanner 的任何调用都将结束程序,不会抛出异常或错误。只调用 .next() 有效。我可以让程序运行,但它会很麻烦,而且我真的不明白发生了什么。我怀疑我错过了一个非常简单的概念,但我迷路了。任何帮助,将不胜感激。
回答by Yogendra Singh
Please make sure you are not closing and re-initializing Scanner stdin
before calling constructor as I suspect that is the problem i.e. if you are doing something like below:
请确保Scanner stdin
在调用构造函数之前没有关闭和重新初始化,因为我怀疑这是问题所在,即如果您正在执行以下操作:
Scanner stdin = new Scanner(System.in);
.........
stdin.close(); //This will close your input stream(System.in) as well
.....
.....
stdin = new Scanner(System.in);
PhDCandidate phDCandidate = new PhDCandidate(stdin);
stdin
inside the constructor will not read anything as input stream System.in
is already closed.
stdin
由于输入流System.in
已经关闭,构造函数内部不会读取任何内容。
回答by PermGenError
your code works fine for me. after creating the scanner in the main pass it as an argument.
你的代码对我来说很好用。在主程序中创建扫描仪后,将其作为参数传递。
public Test(Scanner stdin)
{
System.out.println("enter something");
name = stdin.nextLine();
System.out.println(name); //THIS NEVER RUNS
System.out.print("alfkj");
}
public static void main(String...args)throws SQLException {
new Test(new Scanner(System.in));
}
output: enter something
xyzabc
alfkj
回答by Tejen Shrestha
Add a set Name method in your PhDCandidate class. This way you can create a PhDCandidate object inside your main method and print the name or do whatever from main.
在您的 PhDCandidate 类中添加一个 set Name 方法。通过这种方式,您可以在 main 方法中创建一个 PhDCandidate 对象并打印名称或从 main 中执行任何操作。
public static void main(String[] args) {
PhDCandidate c = new PhDCandidate();
c.setName(stdin.nextLine());
}