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


相关推荐

  • DirectSound的应用

    DirectSound的应用

    2021年12月14日
    38
  • mysql gtid复制_MySQL主从同步

    mysql gtid复制_MySQL主从同步48、mysql基于GTID的主从复制实战

    2022年4月20日
    39
  • RBF神经网络实验原理_神经网络多元拟合

    RBF神经网络实验原理_神经网络多元拟合RBF神经网络及拟合实例RBF神经网络介绍RBF神经网络结构RBF神经网络算法RBF神经网络逼近算法采用RBF神经网络逼近非线性函数神经网络逼近结果代码如下RBF神经网络介绍RBF神经网络结构径向基函数(RadialBasisFunction,RBF)神经网络是一种单隐含层的三层前馈神经网络,网络结构如下图所示RBF神经网络模拟了人脑中局部调整,相互覆盖接受域(或者说感受域,ReceptiveField)的神经网络结构。与BP神经网络相同,研究人员已经证明RBF神经网络能够以任何精度逼近任

    2025年6月1日
    3
  • 利用其它widthStep方法把interest_img的所有像素值增加1[通俗易懂]

    利用其它widthStep方法把interest_img的所有像素值增加1[通俗易懂]#include”stdafx.h”#include#includeintmain(intargc,char**argv){   IplImage*interest_img;   CvRectinterest_rect;   if(argc==7&&((interest_img=cvLoadImage(argv[1],1))!=

    2022年5月6日
    39
  • html文本框鼠标离开事件,html鼠标事件_文本框事件「建议收藏」

    html文本框鼠标离开事件,html鼠标事件_文本框事件「建议收藏」该楼层疑似违规已被系统折叠隐藏此楼查看此楼今天57号网络技术分享给大家介绍html鼠标事件,就是当前文本框获得焦点时,里面的原有文本清零。首先我们来看一看源代码运行后的效果图片:你也可以【点击预览】,看到我们真实的效果。下面是源代码,喜欢就支持一下吧:/p>Transitional//EN””http://www.w3.org/TR/xhtml1/DTD/xhtml1-transition…

    2025年8月8日
    3
  • webrtc技术原理_webrtc开源项目

    webrtc技术原理_webrtc开源项目一、概述webrtc冗余打包方式有三种:Red(rfc2198)、Ulpfec(rfc5109)、Flexfec(草案)。其中Red和Ulpfec要成对使用。二、RedFEC简单将老报文打包到新包上。如下图所示,冗余度为1时,RFC2198打包情况:这种方法在音视频领域几乎不使用,因为冗余包只能保护特定一个报文,这种方法带宽占用量很大,恢复能力有限,性价比很低。只是早期的T38……

    2022年8月11日
    7

发表回复

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

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