欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于java中nextline读取空白行的问题

程序员文章站 2022-06-04 19:29:12
...

最近在做java作业, 发现了一个问题, 就是nextline其实会接收缓冲区的\r, 使得在程序运行时nextline像是跳过了一样, 其实不然, 它只是读取了上一个enter时的\r, 如我的如下功能代码

    public void run() {
        Scanner scan = new Scanner(System.in);
        int ord, book_order;
        int flag = 0;
        String isbn = new String();
        String nme = new String();
        while (flag == 0) {
            System.out.println("Function List:");
            System.out.println("1.Show the book list");
            System.out.println("2.Find book");
            System.out.println("3.Add book");
            System.out.println("4.Delete book");
            System.out.println("0.Exit");
            System.out.println("Please input the functon order:");
            ord = scan.nextInt();
            switch (ord) {
            case 0: {
                flag = 1;
                break;
            }
            case 1: {
                Print();
                break;
            }
            case 2: {
            	Scanner in = new Scanner(System.in);
                System.out.println("Please input the ISBN of book you want to find:");
                isbn = in.nextLine();
                book_order = Find(isbn);
                if (book_order != 0) {
                    System.out.println("Success!");
                    System.out.println("Name:" + books[book_order].name);
                    System.out.println("ISBN:" + books[book_order].ISBN);
                    System.out.println();
                } else {
                    System.out.println("Error!No such book!");
                    System.out.println();
                }
                break;
            }
            case 3: {
            	Scanner in = new Scanner(System.in);
                System.out.println("Please input the name of the book:");
                nme = scan.nextLine();
                System.out.println("Please input the ISBN of the book:");
                isbn = scan.nextLine();
                if (Add(nme, isbn)) {
                    System.out.println("Add successfully!");
                    System.out.println();
                } else {
                    System.out.println("Failed to add!It's out of range!");
                    System.out.println();
                }
                break;
            }
            case 4: {
                System.out.println("Please input the ISBN of book you want to delete:");
                isbn = scan.nextLine();
                if (Del(isbn)) {
                    System.out.println("Delete successfully!");
                    System.out.println();
                } else {
                    System.out.println("Failed to delete!No such book!");
                    System.out.println();
                }
                break;
            }
            }
        }
        scan.close();
    }
}

在这里, 我一开始就已经实例化了一个scanner对象, 命名为scan, 然后在输入功能选项时我们调用了nextInt, 那么在输入完之后, 整数被读取进去了, 但是那个\r还留在scan的buffer区域, 所以, 如果我们添加书籍的话, 再次使用scan的nextline来读取书名的话, nextline就会得到scan的buffer区域中的\r,导致程序错误, 所以,在这里看到老师的代码之后, 得到启发, 重新新建一个scanner对象, 在这里可以使用in

    public void run() {
        Scanner scan = new Scanner(System.in);
        int ord, book_order;
        int flag = 0;
        String isbn = new String();
        String nme = new String();
        while (flag == 0) {
            System.out.println("Function List:");
            System.out.println("1.Show the book list");
            System.out.println("2.Find book");
            System.out.println("3.Add book");
            System.out.println("4.Delete book");
            System.out.println("0.Exit");
            System.out.println("Please input the functon order:");
            ord = scan.nextInt();
            switch (ord) {
            case 0: {
                flag = 1;
                break;
            }
            case 1: {
                Print();
                break;
            }
            case 2: {
            	Scanner in = new Scanner(System.in);
                System.out.println("Please input the ISBN of book you want to find:");
                isbn = in.nextLine();
                book_order = Find(isbn);
                if (book_order != 0) {
                    System.out.println("Success!");
                    System.out.println("Name:" + books[book_order].name);
                    System.out.println("ISBN:" + books[book_order].ISBN);
                    System.out.println();
                } else {
                    System.out.println("Error!No such book!");
                    System.out.println();
                }
                break;
            }
            case 3: {
            	Scanner in = new Scanner(System.in);
                System.out.println("Please input the name of the book:");
                nme = in.nextLine();
                System.out.println("Please input the ISBN of the book:");
                isbn = in.nextLine();
                if (Add(nme, isbn)) {
                    System.out.println("Add successfully!");
                    System.out.println();
                } else {
                    System.out.println("Failed to add!It's out of range!");
                    System.out.println();
                }
                break;
            }
            case 4: {
                System.out.println("Please input the ISBN of book you want to delete:");
                isbn = scan.nextLine();
                if (Del(isbn)) {
                    System.out.println("Delete successfully!");
                    System.out.println();
                } else {
                    System.out.println("Failed to delete!No such book!");
                    System.out.println();
                }
                break;
            }
            }
        }
        scan.close();
    }
}

运行结果如下:
关于java中nextline读取空白行的问题
此外, 也可以和cpp一样, 使用一次scan.nextLine将那个\r读取.

上一篇: 解惑

下一篇: LinkedList 的实现原理