1. Java.util.Scanner可以用来从键盘获取输入

Scanner.next()  只能读取字符,遇到任何的符合都不会输出

Scanner.nextLine()  会完全按照用户输入的string输出

Example1 for Scanner.next():

package com.mengdd.junit;
import java.util.*; public class ScanDemo {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);//get data from keyboard
//get date with nextline
System.out.println("get date with next: ");
if (scan.hasNext()){
String str = scan.next();
System.out.print(str);
}
}
} run this class and input: I am entering a line to test
after running, the output would be:
get date with next:
I
All strings after I will not be outputed Example 2 for Scanner.nextLine():
package com.mengdd.junit;

import java.util.Scanner;

/**
* Created by Sandy.Liu on 2017/6/22.
*/
public class ScanDemo1 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("please enter a string");
if(scan.hasNextLine()){
String str = scan.nextLine();
System.out.print(str);
}
}
} run this class and the result will be:

please enter a string
hellow i am entering a line to test
hellow i am entering a line to test
Process finished with exit code 0

So nextLine() will get every single character the user inputs even the punctuation

最新文章

  1. 控制window.print不打印某些内容
  2. C#查看各种变量的指针地址
  3. JAVA输出图形(网上找的)
  4. again
  5. Codeforces Beta Round #85 (Div. 1 Only) A. Petya and Inequiations 贪心
  6. 【NOIP 2012 开车旅行】***
  7. mysql server install
  8. 一个好用的Python备份mysql的脚本
  9. 屏蔽错误:LNK2038
  10. Java对象序列化
  11. commitProperties方法
  12. java.net.UnknownHostException 异常解决方案
  13. 201521123009 《Java程序设计》第12周学习总结
  14. 使用HTML DOM 来分配事件 —— onmouseover和onmouseout ,onmousedown和onmouseup
  15. HDU 5914 Triangle(打表——斐波那契数的应用)
  16. pat 抢红包
  17. [poj1185]炮兵阵地_状压dp
  18. django_1
  19. Win10升级.NET Framework 3.5或2.0遇到错误0x800f081f怎么办
  20. 复制MIFARE Classic卡

热门文章

  1. 如何正确认识Docker Kubernetes 和 Apache Mesos
  2. unity3d优化总结篇(二)
  3. Effective Java Methods Common to All Objects
  4. final视频
  5. Android修行之路------List view无法获取监听方法
  6. SpringMVC实现从磁盘中下载文件
  7. centos安装nginx和配置
  8. Javascript中的闭包 O__O "…
  9. 适配手机端之 rem
  10. 【转载】 火爆的996.ICU项目正在酝酿开源许可证 禁止996公司使用