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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • activate-power-mode安装与设置

    activate-power-mode安装与设置Windowactivatepowermode去掉combo/shake,其他三个全勾上,现在用起来就很爽了,赶紧体验吧。

    2022年7月1日
    24
  • java当前时间的时间戳_java获取时间戳和当前时间

    java当前时间的时间戳_java获取时间戳和当前时间java中的时间戳是毫秒为单位,13位;unix的时间戳是秒,10位一、java中获取时间戳//方法一System.currentTimeMillis();//方法二Calendar.getInstance().getTimeInMillis();//方法三newDate().getTime();三种方法性能比较:每种方法运行1亿次importjava.text.SimpleDateFo…

    2022年5月3日
    311
  • java 8函数式编程pdf 百度云盘链接

    链接:https://pan.baidu.com/s/1OPd3Cf7NVcxBOguDjWavRw提取码:wakt相关总结可以关注我的博客

    2022年4月12日
    144
  • 数据库的数据模型是网状模型_网状模型的数据结构是

    数据库的数据模型是网状模型_网状模型的数据结构是层次数据模型    定义:层次数据模型是用树状<层次>结构来组织数据的数据模型。    满足下面两个条件的基本层次联系的集合为层次模型    1.有且只有一个结点没有双亲结点,这个结点称为根结点    2.根以外的其它结点有且只有一个双亲结点其实层次数据模型就是的图形表示就是一个倒立生长的树,由基本数据结构中的树(或者二叉树)的定义可知,每棵树都有且仅有一个根节点,其余的…

    2025年6月28日
    1
  • java视频教程免费下载

    java视频教程免费下载Java视频教程孙鑫Java无难事(全12CD)Java视频教程即学即会java上海交大Java初级编程基础共25讲下载avi格式吉林大学Java程序设计视频教程48讲+2讲辅导csf格式尚学堂Java视频教程J2SE5.0马士兵主讲高清avi格式张孝祥Java课堂系列之Tomcat入门(七讲swf)张孝祥Java课堂系列

    2022年5月13日
    31
  • ubuntu android studio_android自启动

    ubuntu android studio_android自启动(1)添加头文件:#include(2)在特定驱动结构体中添加early_suspend结构:#ifdefCONFIG_HAS_EARLYSUSPENDstructearly_suspendearly_suspend;#endif(3)在驱动probe函数中注册相关early_suspend结构体:#ifdefCONFIG_HAS_EARLYSUSPEND

    2022年9月18日
    0

发表回复

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

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