关于java中nextline读取空白行的问题
生活随笔
收集整理的這篇文章主要介紹了
关于java中nextline读取空白行的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近在做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對象, 在這里可以使用Scanner in, 這樣的話, in的buffer區域中就沒有之前存留的\n, 那么這個時候我們再輸入一行, 則不會出現\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 = 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();} }運行結果如下:
此外, 也可以和cpp一樣, 使用一次scan.nextLine將那個\r讀取.
總結
以上是生活随笔為你收集整理的关于java中nextline读取空白行的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ 3564 信号增幅仪
- 下一篇: js基于后台数据实现table行列合并