Scanner工具类

时间:2024-07-12 06:59:54

扫描控制台输入

1.nextLine

   nextLine() 方法会扫描输入流中的字符,直到遇到行末尾的换行符 \n,然后将该行的内容作为字符串返回,同时,nextLine() 会将 Scanner 对象的位置移动到下一行的开头,以便下一次读取数据时从下一行的开头开始读取。

Scanner scanner = new Scanner(System.in); // 创建 Scanner 对象,从标准输入流中读取数据
System.out.println("请输入多行文本,以空行结束:");
StringBuilder sb = new StringBuilder(); // 创建 StringBuilder 对象,用于保存读取的文本
String line = scanner.nextLine(); // 读取输入流中的第一行
while (!line.isEmpty()) { // 如果读取的行不为空,则继续读取下一行
    sb.append(line).append("\n"); // 将当前行的内容添加到 StringBuilder 对象中,并换行
    line = scanner.nextLine(); // 读取下一行
}
System.out.println("您输入的文本是:\n" + sb.toString()); // 打印读取的文本
scanner.close(); // 关闭 Scanner 对象

2.nextInt

   nextInt() 用于从输入流中读取下一个整数并返回,如果输入流中没有整数,或者不是整数,将抛出 InputMismatchException 异常。

3.其他方法

除了以上两个常用的方法,Scanner 类中还有一些其他的方法:

  • boolean hasNext():检查输入流是否还有下一个标记。
  • boolean hasNextLine():检查输入流是否还有下一行。
  • String next():读取输入流中的下一个标记(使用默认的分隔符,通常是空格或换行符)。
  • double nextDouble():读取输入流中的下一个双精度浮点数。
Scanner scanner = new Scanner(System.in); // 创建 Scanner 对象,从标准输入流中读取数据
System.out.print("请输入一个整数:");
if (scanner.hasNextInt()) { // 判断输入流中是否有下一个整数
    int num = scanner.nextInt(); // 读取输入流中的下一个整数
    System.out.println("您输入的整数是:" + num);
} else {
    System.out.println("输入的不是整数!");
}
scanner.nextLine(); // 读取输入流中的换行符

System.out.print("请输入多个单词,以空格分隔:");
while (scanner.hasNext()) { // 判断输入流中是否还有下一个标记
    String word = scanner.next(); // 读取输入流中的下一个单词
    System.out.println("您输入的单词是:" + word);
}
scanner.nextLine(); // 读取输入流中的换行符

System.out.print("请输入一个实数:");
if (scanner.hasNextDouble()) { // 判断输入流中是否有下一个实数
    double num = scanner.nextDouble(); // 读取输入流中的下一个实数
    System.out.println("您输入的实数是:" + num);
} else {
    System.out.println("输入的不是实数!");
}
scanner.nextLine(); // 读取输入流中的换行符

System.out.print("请输入一个字符串:");
if (scanner.hasNextLine()) { // 判断输入流中是否有下一行
    String line = scanner.nextLine(); // 读取输入流中的下一行
    System.out.println("您输入的字符串是:" + line);
} else {
    System.out.println("输入的不是字符串!");
}
scanner.close(); // 关闭 Scanner 对象

扫描文件

try {
    // 创建 File 对象,表示要扫描的文件
    File file = new File("docs/安装环境.md");
    Scanner scanner = new Scanner(file); // 创建 Scanner 对象,从文件中读取数据
    while (scanner.hasNextLine()) { // 判断文件中是否有下一行
        String line = scanner.nextLine(); // 读取文件中的下一行
        System.out.println(line); // 打印读取的行
    }
    scanner.close(); // 关闭 Scanner 对象
} catch (FileNotFoundException e) {
    System.out.println("文件不存在!");
}

         在上面的示例中,我们首先创建了一个 File 对象,表示要扫描的文件。然后,我们使用 Scanner 类的构造方法来创建 Scanner 对象,将文件作为参数传递给构造方法。在 while 循环中,我们使用 hasNextLine() 方法来判断文件中是否有下一行,如果有,则使用 nextLine() 方法读取该行字符串,并使用 println() 方法将其打印出来。最后,我们在程序结束前使用 close() 方法关闭 Scanner 对象.

查找匹配项

 实例:

String input = "good good study, day day up.";
Scanner scanner = new Scanner(input);
String result;

// 使用 findInLine() 方法查找字符串中的单词
result = scanner.findInLine("study");
System.out.println("findInLine(): " + result); // 输出 "study"

// 使用 findWithinHorizon() 方法查找字符串中的单词
scanner = new Scanner(input);
result = scanner.findWithinHorizon("study", 20);
System.out.println("findWithinHorizon(): " + result); // 输出 "study"

scanner.close(); // 关闭 Scanner 对象

        在上面的示例中,我们首先创建了一个字符串 input,表示要查找的文本。然后,我们使用 Scanner 类的构造方法创建 Scanner 对象,并将 input 作为输入流传递给该对象。接着,我们使用 findInLine() 方法和 findWithinHorizon() 方法分别查找字符串中的单词 "study"。其中,findInLine() 方法在当前行中查找匹配项,而 findWithinHorizon() 方法在指定的限制范围内查找匹配项。在本例中,我们将查找的范围限制为前 20 个字符。

        需要注意的是,findInLine() 方法和 findWithinHorizon() 方法都返回找到的匹配项。如果没有找到匹配项,则返回 null。此外,findInLine() 方法和 findWithinHorizon() 方法都会忽略默认的分隔符,因此需要使用正则表达式来指定查找的模式。

小结

        总之,Scanner 类是一个功能强大的输入处理工具类,不仅可以扫描控制台的输入流,还可以扫描文件,并且提供了多种方法来读取不同类型的数据,比如 next()nextInt()nextLine()如, nextDouble() 等。