BufferedReader.nextLine() 不会前进到下一行,所以我陷入了循环

发布于 2025-01-06 19:01:01 字数 3318 浏览 0 评论 0原文

我正在尝试重用 Scanner(System.in)BufferedReader(FileReader) 的输入方法。当用户输入“readfile”作为命令时,程序将文件中的第一个人(即文件中的第一行)添加到列表中,但 BufferedReader 被卡在同一行上,并且因此陷入了无限循环。
键盘输入的 Scanner 方法就像一个魅力。

    class Menu {
        public static final int ARRAYINDEXFOREMAIL = 3;
        private static final String NEWPERSON = "newperson";
        private static final String FROMFILE = "readfile";
        public static final String READFILE = "saved.txt";
        private Scanner keyboardInput = new Scanner( System.in );
        private String name;
        private String tlf;
        private String[] email;
        private String [] stringTmp;
        private String in;
        private boolean readFromFile = false;

        Menu( boolean fileRead ) {
            if( fileRead ) {
                readFromFile = true;
            }
            else {
                readFromFile = false;
            }
        }

        void run() {
            while(true) {
                System.out.print("\n" + "ordre> ");
                if ( !readFromFile ) {
                    in = keyboardInput.nextLine();
                }
                else {
                    try {
                        BufferedReader fileOut = new BufferedReader(
                                new FileReader( READFILE ) );
                        if( ( in = fileOut.readLine() ) == null ) {
                            readFromFile = false;
                            fileOut.close();
                            break;
                        }
                        else {
                            System.out.println("in stringTmp is: " + in);   // For debugging purposes
                        }
                    }
                    catch (FileNotFoundException filenotfoundexception) {
                        System.out.println("Cannot find file: " + READFILE + " !");
                    }
                    catch (IOException ioexception) {
                        ioexception.printStackTrace();
                    }
                }
                stringTmp = in.split("\\s+");

                if( stringTmp[0].equalsIgnoreCase(NEWPERSON) ) {
                    if( stringTmp.length <= 1 ) {
                        System.out.println("ERROR! Enter one or more parameters..");
                        continue;
                    }
                    else {
                        name = stringTmp[1];
                        tlf = stringTmp[2];
                        if( stringTmp.length > ARRAYINDEXFOREMAIL ) {
                            email = new String[stringTmp.length - ARRAYINDEXFOREMAIL];

                            for( int i = ARRAYINDEXFOREMAIL, j = i - ARRAYINDEXFOREMAIL ; i < stringTmp.length; i++, j++ ) {
                                email[j] = stringTmp[i];
                            }
                        }
                        else {
                            email = new String[0];
                        }
                        mainlist.addPerson( name, tlf, email );
                    }
                }

                else if ( stringTmp[0].equalsIgnoreCase(FROMFILE) ) {
                    Meny m = new Menu( true );
                    m.run();
                }
            }
        }
    }

I am trying to reuse input method for both Scanner(System.in) and BufferedReader(FileReader). When the user types "readfile" as a command, the program adds the first person from the file (ie the first line from the file) to a list, but the BufferedReader is stuck on the same line and therefore is stuck in an infinite loop.
The Scanner method from keyboard input works like a charm.

    class Menu {
        public static final int ARRAYINDEXFOREMAIL = 3;
        private static final String NEWPERSON = "newperson";
        private static final String FROMFILE = "readfile";
        public static final String READFILE = "saved.txt";
        private Scanner keyboardInput = new Scanner( System.in );
        private String name;
        private String tlf;
        private String[] email;
        private String [] stringTmp;
        private String in;
        private boolean readFromFile = false;

        Menu( boolean fileRead ) {
            if( fileRead ) {
                readFromFile = true;
            }
            else {
                readFromFile = false;
            }
        }

        void run() {
            while(true) {
                System.out.print("\n" + "ordre> ");
                if ( !readFromFile ) {
                    in = keyboardInput.nextLine();
                }
                else {
                    try {
                        BufferedReader fileOut = new BufferedReader(
                                new FileReader( READFILE ) );
                        if( ( in = fileOut.readLine() ) == null ) {
                            readFromFile = false;
                            fileOut.close();
                            break;
                        }
                        else {
                            System.out.println("in stringTmp is: " + in);   // For debugging purposes
                        }
                    }
                    catch (FileNotFoundException filenotfoundexception) {
                        System.out.println("Cannot find file: " + READFILE + " !");
                    }
                    catch (IOException ioexception) {
                        ioexception.printStackTrace();
                    }
                }
                stringTmp = in.split("\\s+");

                if( stringTmp[0].equalsIgnoreCase(NEWPERSON) ) {
                    if( stringTmp.length <= 1 ) {
                        System.out.println("ERROR! Enter one or more parameters..");
                        continue;
                    }
                    else {
                        name = stringTmp[1];
                        tlf = stringTmp[2];
                        if( stringTmp.length > ARRAYINDEXFOREMAIL ) {
                            email = new String[stringTmp.length - ARRAYINDEXFOREMAIL];

                            for( int i = ARRAYINDEXFOREMAIL, j = i - ARRAYINDEXFOREMAIL ; i < stringTmp.length; i++, j++ ) {
                                email[j] = stringTmp[i];
                            }
                        }
                        else {
                            email = new String[0];
                        }
                        mainlist.addPerson( name, tlf, email );
                    }
                }

                else if ( stringTmp[0].equalsIgnoreCase(FROMFILE) ) {
                    Meny m = new Menu( true );
                    m.run();
                }
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2025-01-13 19:01:01

请注意,在循环本身中,您设置

BufferedReader fileOut = new BufferedReader(new FileReader( READFILE ) );

这将导致创建一个新的 BufferedReader!

在循环的下一次迭代中 - 您将重复此行,重置 fileOut 作为结果 - 当您调用 fileOut.readLine() 时 - 它将读取第一个再次排队。

您应该在循环之前而不是在循环中初始化 fileOut 来解决这个问题。

Note that in the loop itself you set

BufferedReader fileOut = new BufferedReader(new FileReader( READFILE ) );

This will cause a new BufferedReader to be created!

In the loop's next iteration - you will repeat this line, reset fileOut as a result - and when you invoke fileOut.readLine() - it will read the first line again.

You should initialize fileOut before the loop and not in it to solve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文