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

时间:2012-02-16 13:31:06

标签: java bufferedreader

我正在尝试重用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();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:7)

请注意,在循环中你设置

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

这将导致创建新的BufferedReader
在循环的下一次迭代中 - 您将重复此行,重置fileOut作为结果 - 当您调用fileOut.readLine()时 - 它将再次读取第一行。

您应该在循环之前初始化fileOut而不是在其中解决它。