获得NoSuchElementException

时间:2015-10-15 19:03:29

标签: java exception

我已经在这个问题上坚持了很长一段时间,

    public static PureMember createPureMember(){
    Scanner input = new Scanner(System.in);
    PureMember pureMember = new PureMember(xxx(nvm this);
    String tempEntry;

    System.out.println("Enter details of PureMember: \n\n");
    System.out.println("Please enter members first name: ");
    if(input.hasNextLine() )
        pureMember.setfName(input.next() );
    else
        pureMember.setfName(null);

    System.out.println("Please enter members second name: ");
    if(input.hasNextLine() )
        pureMember.setlName(input.next());
    else
        pureMember.setlName(null);

    System.out.println("Please enter members address line 1: ");
    if(input.hasNextLine() )
        pureMember.setAddress1(input.next());
    else
        pureMember.setAddress1(null);

    System.out.println("Please enter members address line 2: ");
    if(input.hasNextLine() )
    pureMember.setAddress2(input.next());
    else
        pureMember.setAddress2(null);

    System.out.println("Please enter members age: ");
    if(input.hasNextLine() )
        pureMember.setAge(input.nextInt());
    else
        pureMember.setAge(0);

    System.out.println("Please enter members date of birth eg: 1-31: ");
    if(input.hasNextLine() )
        pureMember.setD(input.nextInt());
    else
        pureMember.setD(0);

    System.out.println("Please enter members month of birth eg: 1-21: ");
    if(input.hasNextLine() )
        pureMember.setO(input.nextInt());
    else
        pureMember.setO(0);

    System.out.println("Please enter members year of birth eg: 1992 : ");
    if(input.hasNextLine() )
        pureMember.setB(input.nextInt());
    else
        pureMember.setB(0);

    System.out.println("Please enter members height eg: 5.6: ");
    if(input.hasNextLine() )
        pureMember.setHeight(input.nextDouble());
    else
        pureMember.setHeight(0);

    System.out.println("Please enter members weight eg: 11.2: ");
    if(input.hasNextLine() )
        pureMember.setWeight(input.nextDouble());
    else
        pureMember.setWeight(0);

    System.out.println("Please enter members mobile number: ");
    if(input.hasNextLine() )
        pureMember.setMobNumber(input.next());
    else
        pureMember.setMobNumber(null);

    System.out.println("Please enter members home number: ");
    if(input.hasNextLine() )
        pureMember.setHomeNumber(input.next());
    else
        pureMember.setHomeNumber(null);

    System.out.println("Please enter members next of kin name: ");
    if(input.hasNextLine() )
        pureMember.setNextOfKinName(input.next());
    else
        pureMember.setNextOfKinName(null);

    System.out.println("Please enter members next of kin number: ");
    if(input.hasNextLine() )
        pureMember.setNextOfKinNumber(input.next());
    else
        pureMember.setNextOfKinNumber(null);

    System.out.println("Please enter members email:  ");
    if(input.hasNextLine() )
        pureMember.setEmail(input.next());
    else
        pureMember.setEmail(null);

    System.out.println("Please enter members price of membership: ");
    if(input.hasNextLine() )
        pureMember.setPrice(input.nextDouble());
    else
        pureMember.setPrice(0);

    System.out.println("Please enter members accessLevel 1-5: ");
    // Should be integer, accessLevel
    if(input.hasNextLine() )
        pureMember.setAccessLevel(input.nextDouble());
    else
        pureMember.setAccessLevel(0);

    System.out.println("Please enter members access level description: ");
    if(input.hasNextLine() )
        pureMember.setAccessLevelDescription(input.next());
    else
        pureMember.setAccessLevelDescription(null);

    input.close();

    return pureMember;

    }
    }

我有两个菜单,一个主菜单可以选择基本上是CRUD程序。当用户选择1时,程序打印出要创建的成员类型的子菜单,当您在此子菜单中选择一种类型的成员时,将调用此方法,从而创建此类对象。但是,它甚至不允许我输入任何数据,只打印所有的打印语句,然后抛出这个异常。

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at ie.wit.app.MainMenu.createPureMember(MainMenu.java:123)
at ie.wit.app.MainMenu.main(MainMenu.java:46)

我已阅读文档并且不明白,我从先前提出这个问题的人那里得到了hasNextLine(),但对我没有用?

谢谢,

0 个答案:

没有答案