NoSuchElementException出错

时间:2015-10-09 12:29:12

标签: java oracle nosuchelementexception

有人可以帮助我:我收到以下错误:

public class Abc {   
public static Person[] names;
public static void main(String[] args) {
    menuSelection();
}

public static void menuSelection() {

    Scanner s = new Scanner(System.in);
    System.out.println( "Choose menu item:" + "\n" + "1. Read File"
            + "\n" + "2. Creates nr objects" + "\n" + "3. Write a File"
            + "\n" + "4. Display nr objects" + "\n" + "5. Exit");
    int menuSelection = s.nextInt();

    switch (menuSelection) {
    case 1: System.out.println("Input a name");
    String filePerson = s.next();
    PersonRead(filePerson);
    break;

    case 2: System.out.println("Input nbr of obj");
    int p = s.nextInt();
    PersonsCreate(p);
    break;

    case 3: System.out.println("Input a name");
    String filePersonWrite = s.next();
    PersonWrite(names, filePersonWrite);
    break;

    case 4: PersonsDisplay(names);
    break;

    case 5: System.out.println("Good Bye!");
    s.close();
    break;

    default: System.out.println("Invalid choice");
    menuSelection();
    break;
    }
}

public static Person[] PersonRead (String filePerson) {

    Person[] names2 = names;

    try (FileInputStream fi = new FileInputStream(filePerson)) {

        ObjectInputStream os = new ObjectInputStream(fi);

        names2 = (Person[])os.readObject();

        os.close();

    } catch (IOException e) {
        System.out.println("Person file not found.");
        System.out.println(e.getMessage());

    } catch (ClassNotFoundException e) {
        System.out.println("File " + filePerson + " does not contains valid Person object");
    }

    names = names2;
    if (names != null) {
        System.out.println("p Person read successfully from file " + filePerson);
    }
    menuSelection();
    return names;
}   

public static Person[] PersonsCreate(int p) {

    names = new Person[p];
    for(int i=0; i < p; i++) {
        names[i] = new Person("Mr. Tim" + i, 20 + i, 'M');
    }

    menuSelection();
    return names;
}

public static void PersonWrite (Person[] Person, String filePerson) {

    if (names != null) {

    try (FileOutputStream fs = new FileOutputStream(filePerson)) {

        ObjectOutputStream os = new ObjectOutputStream(fs);

        os.writeObject(names);

        os.close();

    } catch (IOException e) {
        e.printStackTrace();
        }
    System.out.println("p Person written successfully to " + filePerson);

    } else {
        System.out.println("Nothing to write.");
    }

    menuSelection();
}

public static void PersonsDisplay(Person[] Person) {

    for(Person names: Person) {
        System.out.println(names);
    }
    menuSelection();
}
}

当我的第一个输入为3时,我的下一个输入是5,这就发生了。看起来我关闭扫描仪时做错了什么?有人能帮帮我吗? 非常感谢。

{{1}}

1 个答案:

答案 0 :(得分:0)

  

NoSuchElementException如果没有更多令牌可用,则由nextElement抛出

NoSuchElementException

使用hasNextInt()

进行检查
if(input.hasNextInt() ){
  int p = s.nextInt();
}