1

我正在编写一个从文本文件读取输入的程序,当它到达文件末尾时,我应该从控制台获取输入。我编写了代码,但它似乎无法正常工作。有人有什么建议吗?

public static void main(String args[]) {
    PQHeap newsFeed = new PQHeap();
    if (args.length < 2) {
        System.out.println("Usage: java NewsFeed inputFile commandFile");
    }
    if (args.length == 2) {
        try {
            BufferedReader input = new BufferedReader(new FileReader(
                    args[0]));
            String line;

            while ((line = input.readLine()) != null) {
                String[] tokens = line.split(",");
                int like = Integer.parseInt(tokens[1]);
                int age = Integer.parseInt(tokens[2]);
                newsFeed.insert(tokens[0], like, age);
            }

            BufferedReader command = new BufferedReader(new FileReader(
                    args[1]));
            BufferedReader commIn = new BufferedReader(
                    new InputStreamReader(System.in));
            boolean done = false;
            String Input = null;
            while (!done) {
                Input = null;
                System.out.println("Enter a command - a, i, l, p or q: ");
                if ((Input = command.readLine()) == null) {
                    Input = commIn.readLine();
                }

                if (Input.length() > 0) {
                    char choice = Input.charAt(0); // strip off option
                                                    // character
                    String remainder = ""; // used to hold the remainder of
                                            // input
                    // trim off any leading or trailing spaces
                    remainder = Input.substring(1).trim();

                    switch (choice) {

                    case 'a':
                        newsFeed.insert(remainder, 0, 1);
                        break;

                    case 'i':
                        newsFeed.incrementDay();
                        break;

                    case 'l':
                        String[] tokens = remainder.split(",");
                        int id = Integer.parseInt(tokens[0]);
                        int likes = Integer.parseInt(tokens[1]);
                        newsFeed.increaseLikes(id, likes);
                        break;

                    case 'p':
                        if (remainder.length() == 0) {
                            newsFeed.displayTop(System.out, newsFeed.size());
                        } else {
                            newsFeed.displayTop(System.out,
                                    Integer.parseInt(remainder));
                        }
                        break;

                    case 'q':
                        // exit if user input 'x'
                        done = true;
                        break;

                    default:
                        System.out.println("Unknown Command");
                        break;
                    }

                }
            }

        } catch (FileNotFoundException e) {
            System.out.println("Cannot find the specified file");
            System.exit(0);
        } catch (IOException e) {
            System.out.println("Error: an input error has occured");
            System.exit(0);
        }
    }
}

但是在我到达文件末尾后,它并没有立即从控制台获取输入。这是到达文件末尾后的输出

Enter a command - a, i, l, p or q: 
Enter a command - a, i, l, p or q: 
Enter a command - a, i, l, p or q: 
Enter a command - a, i, l, p or q: 
Enter a command - a, i, l, p or q: 
Enter a command - a, i, l, p or q: 
Enter a command - a, i, l, p or q: 
4

4 回答 4

4

布尔值,done永远不会更改为true,导致无限循环。

于 2012-12-05T05:47:15.557 回答
0
while (!done) 

你没有更新完成,这是错误的,它永远不会消失

做这样的事情

        while(!done){

            if((Input = command.readLine()) != null) {
              //not end of file
            }
            else
            {//end of file
                System.out.println("Enter a command - a, i, l, p or q: ");
                Input = commIn.readLine();
             }
        }
于 2012-12-05T05:48:28.997 回答
0

您在Enter a command - a, i, l, p or q:检查文件行尾的循环之外打印。因此,它会在读取文件时不必要地将该字符串打印为文件中总行数的时间。检查下面的代码,我希望它有所帮助。

class ReaderTest {
    public static void main(String... args) throws Exception {
        BufferedReader command = new BufferedReader(new FileReader("test.txt"));
        BufferedReader commIn = new BufferedReader(new InputStreamReader(System.in));
        boolean done = false;
        String Input = null;
        while (!done) {
            Input = null;

            if ((Input = command.readLine()) == null) {
                System.out.println("Enter a command - a, i, l, p or q: ");
                Input = commIn.readLine();
            }
        }
    }
}
于 2012-12-05T06:01:05.640 回答
0

实际上代码没有问题,但是我的输入文件有错误导致这些错误

于 2012-12-05T19:06:24.477 回答