为什么不让eclipse让我输入Scanner?

时间:2015-03-14 11:20:36

标签: java eclipse java.util.scanner

我创建了一个程序,它从用户那里获取有关类(其名称,数据成员,方法等)的信息,并使用缩进打印代码。 但是当我在Eclipse中运行它时(版本:Luna Service Release 1a(4.4.1)) 构建ID:20150109-0600),它不允许我输入数据成员的数量并抛出NoSuchElementException。

这是我的代码段: -

     static void initDataMembers(DataMembers[] var, Classes belongTo) {// for
                                                                    // constructor
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter no. of Data Members in class "
            + belongTo.name + " :-");//It doesn't even let me input


        int num = sc.nextInt();


    var = new DataMembers[num];
    if (var.length > 0) {// i.e. if there are variables, init. them with
                            // constructor
        for (int i = 0; i < var.length; i++) {
            System.out.println(belongTo.name + ": Data member " + (i + 1));
            var[i] = new DataMembers(belongTo);
        }
    }
    sc.close();
}

我的输出如此: -

Enter name of the class:-
Xyz
Enter no. of Data Members in class Xyz :-
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at udc.JavaBody.initDataMembers(JavaBody.java:19)
    at udc.Classes.<init>(Classes.java:40)
    at udc.UserDefinedClass.<init>(UserDefinedClass.java:8)
    at udc.UserDefinedClass.main(UserDefinedClass.java:12)

在进一步检查之后,我发现只有在没有任何类型的情况下才会抛出异常。问题是,Eclipse控制台甚至不允许我输入它。这是软件的问题还是我的代码错误?

1 个答案:

答案 0 :(得分:-2)

执行Run As时,请进入运行配置。

在Java运行配置的Arguments选项卡上,传递您的参数(type the input numbers separated by spaces or newline。)