最近在做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读取空白行的问题相关推荐

  1. java中nextLine读取不到的问题

    要求的标准输入一般是"Scanner cin=new Scanner(System.in);". 其实还有更有效率的方法:Scanner cin=new Scanner(new B ...

  2. Java中如何读取文件夹下的所有文件

    问题:Java中如何读取文件夹下的所有文件 Java里面是如何读取一个文件夹下的所有文件的? 回答一 public void listFilesForFolder(final File folder) ...

  3. java MacBook air,macbook pro 与 macbook air 的区别!(前者是高配?java中如何读取主板序列号、硬盘序列号、MAC地址...

    所以我想问的重点是?(本人使用苹果①向用于上网?整体性能呢:air那么薄,那么轻,请问它的硬盘对比pro有什么利弊,对于air我①直用macbook pro,②年换①次,今年由于①③寸的并没有使用i系 ...

  4. java中nextLine(),读取换行符的解决

    一:问题描述 当输入完第一值后,就未能输入后来的字符串 package com.wyj.two;import java.util.Scanner;public class text {public s ...

  5. JAVA中NEXTLINE()与NEXT()的区别

    java中的next()和nextLine()还是有很大区别的. next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自 ...

  6. Java Scanner.nextLine()读取回车问题解决

    Scanner.nextLine读取回车问题解决 问题描述 问题分析 如何处理多出来的换行符 方法一 方法二 实战 问题解答 问题描述 我们在使用java读取键盘输入时,如果先读取一个int变量,再读 ...

  7. java中udi_Java读取.properties配置文件的方法

    java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是 "键=值"的格式,在propert ...

  8. Java 中nextLine()方法没有执行直接跳过解决办法

    使用Java的Scanner类nextLne()方法从显示器输入数据时,nextInt()后面的nextLine()直接跳过没有执行: 截图:第三个输入直接跳过 通过上网的查找我终于发现了问题出在哪里 ...

  9. java中文件读取方式的顺序_java中读取文件的方式

    java中读取文件的方式 经常遇到java中读取文件的方式,有时候需要指定编码,有时候不需要指定编码,被搞的挺晕的,抽时间整理了一下java读取文件的方式,主要是对字符型的处理,二进制的暂时不考虑. ...

最新文章

  1. overflow:hidden;zoom:1 理解转__
  2. TQ2440的学习——UBOOT移植(串口控制台的支持)
  3. linux将mysql的错误连接数_Mysql学习笔记(十二) 并发参数调节
  4. 证书到期了_注意!出口沙特所有扁钢制品证书将于8月26日到期 ,需要强制QM
  5. python完全支持面向对象编程_python面向对象编程----009
  6. 用Python进行屏幕截图,只用两行代码搞定
  7. MongoDB学习探讨
  8. WCF创建Rest服务(附:.net2.0创建Rest服务)
  9. 适应网络伦理关系要求的新的伦理观
  10. 对三款软件的测评、分析和建议
  11. 产品规划三板斧:商业画布/精益画布/SWOT分析
  12. python生成词云图_python生成词云图
  13. 自定义video的controls
  14. 获取java可用时区列表ZoneId
  15. 史上最全网络安全面试题合集
  16. 幼儿园班级信息管理系统
  17. Elasticsearch许可证过期导致ES用不了的问题
  18. Android VR 全景图
  19. 各类排序算法汇总及动画演示(C语言)
  20. 绘图---PS使用教程总结(一)基本操作

热门文章

  1. WPF自定义控件 —— 装饰器
  2. You get a dream...you gotta protect it.
  3. php梯度区间计算,快速计算梯度的魔法--反向传播算法
  4. 《实现领域驱动设计》读书笔记
  5. windows环境下nginx的入门配置跳转tomcat
  6. android 圆形头像,自定义圆形ImageView
  7. 3650m5服务器内存选择 ibm_各大品牌服务器租用价格表明细(附详细表单)
  8. linux vi命令 置顶,[置顶] Linux vi命令 创建文件
  9. scp会覆盖同名文件吗_你会Hypermesh一键式完成几何文件到求解文件的输出吗?
  10. android webview 图表,Android WebView 无法正常显示网页图表