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