Java中Scanner的nextInt(),next(),nextLine()方法总结[通俗易懂]

Java中Scanner的nextInt(),next(),nextLine()方法总结[通俗易懂]前言:借别人的例子做个总结。原文出处:http://www.cnblogs.com/gold-worker/archive/2013/04/10/3013063.html代码一packagecn.dx;importjava.util.Scanner;publicclassScannerTest{publicstaticvoidmain(String[]

大家好,又见面了,我是你们的朋友全栈君。

前言:借别人的例子做个总结。
原文出处:http://www.cnblogs.com/gold-worker/archive/2013/04/10/3013063.html

###代码一

 package cn.dx;
 import java.util.Scanner;
 public class ScannerTest { 
   
 
     public static void main(String[] args) { 
   
         Scanner in =  new Scanner(System.in);
         System.out.println("请输入一个整数");
         while(in.hasNextInt()){ 
   
             int num = in.nextInt();
             System.out.println("请输入一个字符串");
             String str = in.nextLine();
             System.out.println("num="+num+",str="+str);
             System.out.println("请输入一个整数");
         }
     }
 }

###结果一

请输入一个整数
1231
请输入一个字符串
num=1231,str=
请输入一个整数

第二个String类型的参数没有读取进来。

自己查看了下nextInt()和nextLine()方法的官方文档

  nextLine()

  Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. 

  nextInt()方法会读取下一个int型标志的token.但是焦点不会移动到下一行,仍然处在这一行上。当使用nextLine()方法时会读取改行剩余的所有的内容,包括换行符,然后把焦点移动到下一行的开头。所以这样就无法接收到下一行输入的String类型的变量。

###代码二

 package cn.dx; 
 import java.util.Scanner;
 public class ScannerTest { 
   
 
     public static void main(String[] args) { 
   
         Scanner in =  new Scanner(System.in);
         System.out.println("请输入一个整数");
         while(in.hasNextInt()){ 
   
             int num = in.nextInt();
             System.out.println("请输入一个字符串");
             String str = in.next();
             System.out.println("num="+num+",str="+str);
             System.out.println("请输入一个整数");
         }
     }
 }

###结果二
请输入一个整数
123
请输入一个字符串
sdjakl
num=123,str=sdjakl

请输入一个整数
213 jdskals
请输入一个字符串
num=213,str=jdskals
请输入一个整数

##总结:

Scanner(InputStream in)

constructs a Scanner object from the given input stream.

String nextLine()

reads the next line of input.

String next()

reads the next word of input (delimited by whitespace).

int nextInt()

double nextDouble()

read and convert the next character sequence that represents an
integer or floating-point number.

boolean hasNext()

tests whether there is another word in the input.

boolean hasNextInt()

boolean hasNextDouble()

test whether the next character sequence represents an integer or
floating-point number.


本系列文章

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134319.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据库四大特性及数据库隔离级别

    数据库四大特性及数据库隔离级别MySql本篇文章主要介绍数据库的四大特性ACID,以及说明一下数据库的隔离级别。如果想要说明一个数据库或者一个框架支持事务性操作,则必须要满足下面的四大特性1.原子性(Atomicity)原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚。失败回滚的操作事务,将不能对事物有任何影响。2.一致性(Consistency)一致性是指事务必须使数据库从一个一致性状态变换到另一个一致性状态,…

    2022年5月26日
    36
  • 如何查看CentOS操作系统版本「建议收藏」

    如何查看CentOS操作系统版本「建议收藏」1、如何查看已安装的CentOS版本信息:第一种方式:cat/proc/version第二种方式:uname-a第三种方式:uname-rLinuxversion3.10.0-1127.el7.x86_64(mockbuild@kbuilder.bsys.centos.org)(gccversion4.8.520150623(RedHat4.8.5-39)(GCC))#1SMPTueMar3123:36:51UTC20202.、如何查看linu

    2022年6月24日
    46
  • Arcgis地图切片以及发布

    Arcgis地图切片以及发布Arcgis 地图切片以及发布过程简介地图切片地图切片主要是为了提高地图的浏览速度 可以在地图发布之前首先进行切片 也可以在地图发布时直接利用缓存切片 后者在发布大型地图时不建议采用 速度很慢 1 1 利用工具 arcgis gt Datamanageme gt Tilecache gt Generatetile 生成 xm

    2025年8月13日
    4
  • 微服务架构-实现技术之具体实现工具与框架6:Spring Cloud Hystrix原理与注意事项

    微服务架构-实现技术之具体实现工具与框架6:Spring Cloud Hystrix原理与注意事项目录一、SpringCloudHytrix概述和设计目标(一)SpringCloudHytrix基本概述(二)SpringCloudHytrix概述设计目标二、SpringCloudHytrix解决的主要内容(一)隔离(线程池隔离和信号量隔离)1.线程和线程池线程隔离的好处:线程隔离的缺点2.信号量隔离(Semaphores)(二)优雅的降级…

    2022年6月18日
    22
  • instant java,关于java:Format Instant to String

    instant java,关于java:Format Instant to String我正在尝试使用新的 java8time api 和模式将 Instant 格式化为 String Instantinsta Stringout DateTimeForm ofPattern yyyy MM ddHH mm ss format instant 使用上面的代码我得到一个异常 它抱怨一个不受支持的字段 java time temporal Unsup

    2025年7月6日
    2
  • vue父组件操作子组件的方法_vue子组件向父组件传值的三种方式

    vue父组件操作子组件的方法_vue子组件向父组件传值的三种方式父组件和子组件我们经常分不清什么是父组件,什么是子组件。现在来简单总结下:我们将某段代码封装成一个组件,而这个组件又在另一个组件中引入,而引入该封装的组件的文件叫做父组件,被引入的组件叫做子组件。具

    2022年8月7日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号