Introduction
Problem will appear when the method——next and the nextLine used at the same time in the java source.
What is the next() and the nextLine() ?
Chinese key words
这里讨论的是关于java scanner类中的next方法和nextLine方法.
Problem
Firstly, let us see the code as follows——a simple example of input/output use method next only.
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.nextLine();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
ni = cin.nextLine();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
Then,nextLine():
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.next();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
ni = cin.next();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
The codes above-mentioned are right but silly.But,when you run the follow code,you will have a problem.
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.next();
System.out.println("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
ni = cin.nextLine();
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
Runtime result:
ni掌柜
用Scanner类的next()方法接收了你刚刚输入的字符串是:ni掌柜 长度是4
用Scanner类的next()方法接收了你刚刚输入的字符串是: 长度是0
you will find that when you use the next() and the nextLine() at the same time ,you have no chance to input the data which the nextLine() method will scan.
Analysis
To put it simply, differences between next method and the nextLine is that the former scan a continuous character while the nextLine scans all character in one line.
The reason why problem above-mentioned is that the non_responsible of the method next().
As the method work, it scan the line where there is a cursor,but stops when it meet some particular characte,such as space or a line changing sign,and cut the character that has scanned.
Let’s look into the result above-mentioned, the first method next() have scanned the character of ‘ni掌柜’, but it has not scanned the character of ‘\n’, what’s more, it has not been responsible for the line changing. For the result, it left the character of ‘\n’ to nextLine() for executing.
As we know that , “only the money appear will the method nextLine() open its eye”, so ,nextLine() will stop scanning as soon as it has scanned the character of ‘\n’ , and there are the characters which in before the ‘/n’ in that line, that is,an empty character. Then, problem appears.
Solution
package cn.nileader.app.vi;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception{
Scanner cin = new Scanner(System.in);
String ni = cin.next();
System.out.println("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
while( (ni = cin.nextLine()).equals("") ){}
System.out.print("用Scanner类的next()方法接收了你刚刚输入的字符串是:"+ni+" 长度是"+ni.length());
}
}
Test result:
ni掌柜
用Scanner类的next()方法接收了你刚刚输入的字符串是:ni掌柜 长度是4
是个天才
用Scanner类的next()方法接收了你刚刚输入的字符串是:是个天才 长度是4
just use the program “while( (ni = cin.nextLine()).equals("") ){}” to replace the “ cin.nextLine() ”
分享到:
相关推荐
关于java scanner类中的next方法和nextLine方法。
介绍next与nextLine如何使用以及它们之间的区别。
java中Scanner类nextLine()和next()的区别和使用方法 java中Scanner类nextLine()和next()的区别和使用方法
主要介绍了java中next与nextLine用法区别以及实例分析了他们的区别,需要的朋友可以参考下
python库。 资源全名:nextline-0.1.0.tar.gz
python库。 资源全名:nextline_graphql-0.1.0-py3-none-any.whl
* and open the template in the editor. */ package rsa; import java.math.BigInteger; import java.util.Scanner; import java.util.Random; /** * * @author Administrator */ public class RSABigInteger...
主要介绍了Java Scanner类用法及nextLine()产生的换行符问题,结合实例形式分析了Scanner类功能、hasNextInt()和nextInt()方法使用及nextLine()产生的换行符问题解决方法,需要的朋友可以参考下
接下来我们演示一个简单的的数据输入,并通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前我们一般需要 使用 hasNext 与 hasNextLine 判断是否还有输入的数据: 使用 next 方法: ...
通过对Scanner类中next(),nextInt(),nextLine()方法的分析,可以帮助我们解决一些常见的控制台输入问题
接下来,我们将演示一个最简单的数据输入,并通过Scanner类的next ()与 nextLine() 方法来获取输入的字符串. 下面的创建 Scanner 对象的基本语法: Scanner sc = new Scanner(System.in); next()
Java核心第1天,从导入java.util.Scanner中扫描程序使用next(),nextLine(),nextDouble()
文件行读取器异步逐行文件读取器。...// Reads the file line of the longfile.txt filereader.nextLine(function (err, line) { if (err) { // file not found, no read rights, .. } console.log(line)
Creates a Parser object with the location of the resource (URL or file) You would typically create a DefaultHTMLParserFeedback object and pass it in. Parser(URLConnection connection) Construct a ...
// TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.print("DOS: >"); String command = sc.nextLine(); //split()方法按照指定的分隔符拆分字符串为字符数组 String...
如下所示: ... if nextline== and scan.poll()!=None: break 以上这篇python实时获取外部程序输出结果的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。
这是Java示例项目,涵盖了大部分Java核心知识,并演示了诸如原始数据类型,注释,类,导入,Scanner,final,Strings,static,private,protected,public,构造函数,数学,hasNextLine,nextLine,getters等概念...
println(“ The largest number is : “ + largestNumber(number1, number2)) 创建一个largestNumber方法,该方法将返回传入的2中最大的数字匹配密码检查器给定一个主要功能: Scanner scanInput = new Scanner ( ...