如何避免java.util.NoSuchElementException

时间:2014-10-19 22:12:33

标签: java.util.scanner

下面的代码在我按Ctrl + Z后立即给出了错误java.util.NoSuchElementException 表示用户输入已完成。看起来好像它不知道如何在不弄乱其他扫描仪对象的情况下结束一个方法。 我尝试了hasNext方法,最后我得到了一个无限循环,无论哪种方式都不起作用。作为此分配的要求,我需要能够告诉用户使用Ctrl + Z或D,具体取决于操作系统。此外,我需要能够从文本文件中读取并将最终树保存到文本文件,请帮助。

    /* sample input:
       CSCI3320
       project
       personal
       1 HW1
       1 HW2
       1 2 MSS.java
       2 p1.java

        */

    import java.util.Scanner;
    import java.util.StringTokenizer;

    public class Directory {

        private static TreeNode root = new TreeNode("/", null, null);

        public static void main(String[] args) {
            userMenu();
            System.out.println("The directory is displayed as follows:");
            root.listAll(0);

        }

        private static void userMenu(){         //Displays users menu
            Scanner userInput = new Scanner(System.in);//Scanner option 
            int option = 0;
            do{ //I believe the problem is here since I am not using userInput.Next()
                System.out.println("\n 1. add files from user inputs ");
                System.out.println("\n 2. display the whole directory ");
                System.out.println("\n 3. display the size of directory ");
                System.out.println("\n 0. exit");
                System.out.println("\n Please give a selection [0-3]: ");
                option = userInput.nextInt();
                    switch(option){
                        case 1: addFileFromUser();
                                break;
                        case 2: System.out.println("The directory is displayed as follows:");
                                root.listAll(0);
                                break;
                        case 3: System.out.printf("The size of the directory is %d.\n", root.size());
                                break;
                        default:
                                break;
                        }
            }while( option !=0);
            userInput.close();

        }
        private static void addFileFromUser() {

            System.out.println("To terminate inp1ut, type the correct end-of-file indicator ");
            System.out.println("when you are prompted to enter input.");
            System.out.println("On UNIX/Linux/Mac OS X type <ctrl> d");
            System.out.println("On Windows type <ctrl> z");
            Scanner input = new Scanner(System.in);

                while (input.hasNext()) { //hasNext being used Crtl Z is required to break
                    addFileIntoDirectory(input); // out of the loop.
                }
                input.close();
        }


        private static void addFileIntoDirectory(Scanner input) {

            String line = input.nextLine();
            if (line.trim().equals("")) return;

            StringTokenizer tokens = new StringTokenizer(line);

            int n = tokens.countTokens() - 1;

            TreeNode p = root;
            while (n > 0 && p.isDirectory()) {

                int a = Integer.valueOf( tokens.nextToken() );
                p = p.getFirstChild();

                while (a > 1 && p != null) {
                    p = p.getNextSibling();
                    a--;
                }
                n--;
            }

            String name = tokens.nextToken();

            TreeNode newNode = new TreeNode(name, null, null);
            if (p.getFirstChild() == null) {
                p.setFirstChild(newNode);
            }
            else {
                p = p.getFirstChild();
                while (p.getNextSibling() != null) {
                    p = p.getNextSibling();
                }
                p.setNextSibling(newNode);
            }

        }


        private static class TreeNode {

            private String element;
            private TreeNode firstChild;
            private TreeNode nextSibling;

            public TreeNode(String e, TreeNode f, TreeNode s) {
                setElement(e);
                setFirstChild(f);
                setNextSibling(s);
            }


            public void listAll(int i) {

                for (int k = 0; k < i; k++) {
                    System.out.print('\t');
                }

                System.out.println(getElement());

                if (isDirectory()) {
                    TreeNode t = getFirstChild();

                    while (t != null) {

                        t.listAll(i+1);
                        t = t.getNextSibling();
                    }

                }
            }


            public int size() {

                int s = 1;

                if (isDirectory()) {
                    TreeNode t = getFirstChild();

                    while (t != null) {
                        s += t.size();
                        t = t.getNextSibling();
                    }
                }

                return s;
            }


            public void setElement(String e) {
                element = e;
            }


            public String getElement() {
                return element;
            }


            public boolean isDirectory() {
                return getFirstChild() != null;
            }

            public void setFirstChild(TreeNode f) {
                firstChild = f;
            }       

            public TreeNode getFirstChild() {
                return firstChild;
            }

            public void setNextSibling(TreeNode s) {
                nextSibling = s;
            }       

            public TreeNode getNextSibling() {
                return nextSibling;
            }


        }



    }

异常详细信息:

    /*Exception in thread "main" java.util.NoSuchElementException
            at java.util.Scanner.throwFor(Scanner.java:907)
            at java.util.Scanner.next(Scanner.java:1530)
            at java.util.Scanner.nextInt(Scanner.java:2160)
            at java.util.Scanner.nextInt(Scanner.java:2119)
            at Directory.userMenu(Directory.java:36)
            at Directory.main(Directory.java:21)*/

1 个答案:

答案 0 :(得分:0)

你的问题就在这一行:

option = userInput.nextInt();  //line 24

如果您阅读了Javadoc,您会发现如果输入用尽,nextInt()方法可以抛出NoSuchElementException。换句话说,没有下一个整数要获得。为什么会在您的代码中发生这种情况?因为在第一次迭代完成后(在外部while循环上),此行处于循环中,所以您的初始输入选择已被消耗。由于这是一个家庭作业,我不会写代码。但是,如果你删除循环,你知道这至少工作一次。一旦你尝试循环,它就会中断。所以我会给你这些提示:

  1. 将do / while更改为while循环。
  2. 在循环外提示用户。
  3. 重新创建提示并重新获取循环内的用户输入。
  4. 例如,下面的代码可以用作外部循环的基础。

    import java.util.Scanner;
    
    public class GuessNumberGame {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Guess the secret number: (Hint: the secret number is 1)");
            int guess = input.nextInt();
    
            while (guess != 1) {
                System.out.println("Wrong guess. Try again: ");
                guess = input.nextInt();
            }
            System.out.println("Success");
            input.close();
        }
    }
    

    这之所以有效,是因为我没有重复使用相同的,耗尽的扫描仪输入对象来获取下一个整数。在您的示例中,初始输入位于循环内部。第二次,该输入已被消耗。按照这种模式,您应该能够完成作业。祝你好运!