Scanner类
Scanner
类
来自包 java.util.Scanner
它的功能非常简单。就像真正的扫描仪一样,它从您指定的来源读取数据
。
例如,一个字符串、一个文件、控制台。接下来,它识别信息并对其进行适当的处理。这是最简单的例子:
点击查看代码
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner("床前明月光\n" +
"疑是地上霜\n" +
"举头望明月\n" +
"低头思故乡");
String s = scanner.nextLine();
System.out.println(s);
}
}
上面代码创建了一个Scanner对象,并制定了它的数据源(一串文本)。nextLine()
方法访问数据源(四行文本),找到下一个未读行,并返回它,然后再控制台上输出显示:
床前明月光
可以继续使用nextLine()
多次来显示整首诗.
点击查看代码
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner("床前明月光\n" +
"疑是地上霜\n" +
"举头望明月\n" +
"低头思故乡");
String s = scanner.nextLine();
System.out.println(s);
s = scanner.nextLine();
System.out.println(s);
s = scanner.nextLine();
System.out.println(s);
s = scanner.nextLine();
System.out.println(s);
}
}
床前明月光
疑是地上霜
举头望明月
低头思故乡
Scanner的数据源不一定是字符串,例如,它可以是控制台的输入的数据:
点击查看代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个数字:");
int number = sc.nextInt();
System.out.println("您输入的数字是: " + number);
}
nextInt()
方法读取并返回输入的数字,再如上代码中,我们使用它为变量number赋值,这个程序要求用户输入任何数字,用户操作完成并按回车后,程序会输出结果.
但是用户可能会输入数字之外的内容,例如字符串,这会导致程序停止工作.
点击查看代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个数字:");
int number = sc.nextInt();
System.out.println("您输入的数字是: " + number);
}
当我们尝试输入字符串而不是数字时,控制台的数据如下:
输入一个数字:
我偏不
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:10) Process finished with exit code 1
这时,程序出现了异常,告诉我们输入的内容和程序不匹配,我们就需要一种方法来验证用户输入的数据是否符合预期.
例如,用户输入数字以外的任何内容,最好显示"输入的信息不是数字"警告。如果信息没问题,那么我们可以继续。但这需要我们“预测输入”,看看我们的信息流中会发生什么。
Scanner可以这样做吗?
它有很多方法可以做到这一点:
hasNextInt()
— 这个方法检查下一个输入数据块是否是一个数字(返回真或假,视情况而定)。
hasNextLine()
— 此方法检查下一个输入块是否是字符串。
hasNextByte()
, hasNextShort()
, hasNextLong()
, hasNextFloat()
,hasNextDouble()
— 这些方法都对剩余的数据类型执行类似的检查。
更改数字读取程序如下:
点击查看代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个数字:");
if (sc.hasNextInt()) {
int number = sc.nextInt();
System.out.println("你输入的数字是: " + number);
} else {
System.out.println("您输入的不是数字,请重新启动这个程序再试一次.");
}
}
现在,程序会检查输入的下一个字符是否为数字,只有在它是数字时才会显示正确的内容,如果输入没有通过程序检查,程序会记录下来,并要求用户再试一次.我们可以与Scanner对象进行通信,并提前找出你期待的数据类型.是数字?字符串?还是其他什么,一串数字?什么样的数字? 是int/short/long? 这种灵活性能使你可以构建基于用户行为的程序逻辑.
useDelimiter()
将一个字符串传递给该方法,该字符串为你要作为分割字符串的分隔符或界定符.
例:
点击查看代码
public static void main(String[] args) {
Scanner scan = new Scanner("On a withered branch'" +
"A crow has alighted.'" +
"Nightfall in autumn." +
"''***''" +
"Such a moon above,'" +
"Like a tree cut at the root:'" +
"he fresh cut is white." +
"''***''" +
"How the river floods!'" +
"A heron wanders on short legs,'" +
"Knee-deep in the water.");
scan.useDelimiter("'");
while (scan.hasNext()) {
System.out.println(scan.next());
}
scan.close();
}
使用 ' 作为分隔符,可以得到如下输出
On a withered branch
A crow has alighted.
Nightfall in autumn.
***
Such a moon above,
Like a tree cut at the root:
The fresh cut is white.
***
How the river floods!
A heron wanders on short legs,
Knee-deep in the water.
在这个例子中,我们使用了close()
方法,与处理I/O流的任何对象一样,Scanner在使用完成后必须关闭,以便停止消耗计算机资源. 不要忘记close()方法.