Scanner
Scanner
Scanner类
,可以实现键盘输入数据,到程序中.
java.util.Scanner
是Java5
的新特征,我们可以通过 Scanner
类来获取用户的输入。
下面是创建 Scanner
对象的基本语法:
Scanner s = new Scanner(System.in);
通过 Scanner
类的 next()
与nextLine()
方法获取输入的字符串,在读取前我们一般需要 使用 hasNext
与hasNextLine
判断是否还有输入的数据:
next()
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// 从键盘接收数据
Scanner scan = new Scanner(System.in);
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
next方式接收:
runoob com
输入的数据为:runoob
可以看到 com 字符串并未输出,接下来我们看 nextLine
。
nextLine()
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
nextLine方式接收:
runoob com
输入的数据为:runoob com
next()与nextLine()区别
next()
:
- 一定要读取到
有效字符
后才可以结束输入。 - 对输入有效字符之前遇到的
空白
,next() 方法会自动将其去掉
。 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next()
不能得到带有空格
的字符串。
nextLine()
:
- 以
Enter
为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。 - 可以获得
空白
。
如果要输入 int
或float
类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx()
方法进行验证,再使用 nextXxx()
来读取:
nextInt()
import java.util.Scanner;
public class MaxMap {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
String str = cin.nextLine();
System.out.println("END");
}
}
nextInt()
只读取数值,剩下"\n"
还没有读取,并将cursor
放在本行中。nextLine()
会读取"\n"
,并结束.如果想要在nextInt()
后读取一行,就得在nextInt()
之后额外加上cin.nextLine()
,代码如下
import java.util.Scanner;
public class MaxMap {
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
cin.nextLine();
String str = cin.nextLine();
System.out.println("END");
}
}
读取数字
也可以使用nextLine()
,不过需要转换:
Integer.parseInt(cin.nextLine())
。
注意在next()
、nextInt()
与nextLine()
一起使用时,next()
、nextInt()
往往会读取部分数据
(会留下"\n"或者空格之后的数据
)。
Scanner读取文件
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* 利用Scanner读取项目中的文档
* */
public class TestScannerRead {
public static void main(String[] args){
Scanner sc;
try {
//读取项目中的文档
sc = new Scanner(new File("ScannerTest.txt"));
System.out.println("项目中的文本内容是:");
//通过判断是否有下一行来输出文档内容
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
int i = 0;
float f = 0.0f;
System.out.print("输入整数:");
if (scan.hasNextInt()) {
// 判断输入的是否是整数
i = scan.nextInt();
// 接收整数
System.out.println("整数数据:" + i);
} else {
// 输入错误的信息
System.out.println("输入的不是整数!");
}
System.out.print("输入小数:");
if (scan.hasNextFloat()) {
// 判断输入的是否是小数
f = scan.nextFloat();
// 接收小数
System.out.println("小数数据:" + f);
} else {
// 输入错误的信息
System.out.println("输入的不是小数!");
}
scan.close();
}
}
为了进一步理解next()
,nextLine()
区别,通过下面例子分析
import java.util.Scanner;
//Scanner中nextLine()方法和next()方法的区别
public class ScannerString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入字符串(next):");
String str = input.next();
System.out.println(str);
System.out.println("请输入字符串(nextLine):");//abc
String str1 = input.nextLine();
System.out.println(str1);
}
}
请输入字符串(next):
abc
abc
请输入字符串(nextLine):
输入nextLine
字符串之前,程序已停掉.Scanner
是一个扫描器
,我们录取到键盘的数据,先存到缓存区
等待读取,它判断读取结束
的标示是 空白符
;比如空格,回车,tab
等等。
next()
方法读取到空白符就结束
;
nextLine()
读取到回车结束
也就是“\r”
;
next()
读取到空白符前的数据时结束了,然后把回车“\r”
留给了nextLine()
;所以上面nextLine()没有输出
,不输出不代表没数据,是接到了空(回车“/r”)的数据
。
OJ输入
在Java5
中引入了Scanner
库,这个库在日常使用中是非常方便的,可以直接将输入按照格式读取,但是,效率要比BufferedReader
差很多,因此,在做题时推荐还是用BufferedReader
和InputStreamReader
来使用。
在这些平台,一般要求主类名为Main
,然后将方法写到main()方法中
,因此,我们的类大概是类似如下的格式:
基本格式
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
......
}
}
输入为一个字符串
abcd
代码
// 创建一个BufferedReader对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 读取字符串
String line = br.readLine();
// 测试输入是否正确
System.out.println(line);
输入为多个数字
1 2
代码
// 创建一个BufferedReader对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 读取第一行数据
String line = br.readLine();
// 将字符串根据空格进行分隔
String[] strings = line.trim().split(" ");
// 分别将其中的每个数值读出
int n = Integer.parseInt(strings[0]);
int v = Integer.parseInt(strings[1]);
// 测试输入是否正确
System.out.println("n: " + n + "\tv: " + v);
输入中有一个数组,且有数组的长度
7 6
1 2 3 4 5 6 7
代码
// 创建一个BufferedReader对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 读取第一行数据
String line = br.readLine();
// 将字符串根据空格进行分隔
String[] strings = line.trim().split(" ");
// 分别将其中的每个数值读出
int n = Integer.parseInt(strings[0]);
int v = Integer.parseInt(strings[1]);
// 读取第二行数据
line = br.readLine();
strings = line.trim().split(" ");
// 创建一个int型的数组用来储存第二行的多个数字
int[] nums = new int[n];
for (int i = 0; i < n; i ++) {
nums[i] = Integer.parseInt(strings[i]);
}
// 测试输入是否正确
for (int num: nums) {
System.out.print(num + " ");
}
输入不定组数数据
输入多组数据,不告知组数,也没有截止符
Scanner s=new Scanner(System.in);
while(s.hasNext()){//判断是否数据结束
int a=s.nextInt();
int b=s.nextInt();
}
练习
求a+b
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {//注意while处理多个case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
求n阶方阵中所有数
Input:
3
1 2 3
2 1 3
3 2 1
Output:
18
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans = 0, x;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
x = sc.nextInt();
ans += x;
}
}
System.out.println(ans);
}
}