• Java-Scanner用法


    Scanner是一个专门进行输入流处理的程序类,这个类可以方便处理各种数据类型。Scanner类的主要的方法有

    1. 判断是否有指定类型数据: public boolean hasNextXxx()

    2. 取得指定类型的数据: public 数据类型 nextXxx()

    3. 定义分隔符:public Scanner useDelimiter(Pattern pattern)

    4. 构造方法:public Scanner(InputStream source)

    1.Scanner读取数据有三种方法:

    1.1第一种使用方法:Scanner从控制台读取数据

    基本语法:Scanner input=new Scanner(System.in);

    范例:

    1. import java.io.FileNotFoundException;
    2. import java.util.Scanner;
    3. public class ScannerTest {
    4. public static void main(String[] args) throws FileNotFoundException {
    5. //从键盘中读入时
    6. Scanner input = new Scanner(System.in);
    7. System.out.println("使用nextLine接收内容:");
    8. if (input.hasNext() == true)//判断用户有没有输入字符串
    9. {
    10. String str = input.nextLine();
    11. System.out.println("输出内容:" + str);
    12. }
    13. input.close();
    14. }
    15. }

    1.2第二种使用方法:Scanner从文件种读取数据

    基本语法:Scanner input=new Scanner(new File(fileName));

    范例:

    1. import java.io.File;
    2. import java.io.FileNotFoundException;
    3. import java.util.Scanner;
    4. public class ScannerTest {
    5. public static void main(String[] args) throws FileNotFoundException {
    6. //从文件读取数据
    7. File file=new File("D:\\测试文夹\\aa.txt");
    8. Scanner scanner=new Scanner(file);
    9. while(scanner.hasNext())
    10. {
    11. System.out.println(scanner.next());
    12. }
    13. input.close();
    14. }
    15. }

    1.3从web读取数据

    可以从文件种读取也可以从web服务器中读取:

    URL:统一资源定位器,web文件的唯一地址

    URL url = new URL("www.google.com/index.html");

    创建URL对象以后,使用URL类的openStream()方法获得输入流,使用这个输入流产生Scanner对象,如下

    Scanner reader = new Scanner(url.openStream(),”utf-8”);

    通过reader顺序读,获得资源中的文本数据。

    范例:

    1. import java.io.IOException;
    2. import java.net.URL;
    3. import java.util.Scanner;
    4. public class ScannerTest {
    5. public static void main(String[] args) throws IOException {
    6. URL url=new URL("https://blog.csdn.net/xxxx/article/details/124229293");
    7. Scanner reader = new Scanner(url.openStream(),"utf-8");
    8. while(reader.hasNext()){
    9. System.out.println(reader.nextLine());
    10. }
    11. reader.close();
    12. }
    13. }

    2.next和nextLine的区别

     一般都是通过Scanner类的next()与nextLine()方法获取输入的字符串在读取前一般通过hasNext()和hasNextLine()判断是否还有输入的数据,此时这俩种方法的区别是:

    1.next:

    1. 一定要读取到有效字符后才可以结束输入
    2. 对输入有效字符前的空白,next()方法会自动将其去掉
    3. 输入有效字符后会将其后面输入的空格作为分隔符或者结束符
    4. 根据3可以注意next()不能得到带有空格的字符串

    2.nextLine:      

    1. 以回车为结束符,返回的是输入回车之前的所有内容
    2. 带有空格的字符串可以正常下发

    代码展示:

    1.nextLine的展示:

    1. Scanner input = new Scanner(System.in);
    2. System.out.println("使用nextLine接收内容:");
    3. if (input.hasNext() == true)//判断用户有没有输入字符串
    4. {
    5. String str = input.nextLine();
    6. System.out.println("输出内容:" + str);
    7. }

    结果:

     2.next的展示:

    1. Scanner input = new Scanner(System.in);
    2. System.out.println("使用next接收内容:");
    3. if (input.hasNext() == true)//判断用户有没有输入字符串
    4. {
    5. String str = input.next();
    6. System.out.println("输出内容:" + str);
    7. }

    结果:

     通过结果可以对比出区别。

    3.Scanner可以指定任意 符号,字符等作为分割符;

    范例:

    1. String s = "you are Beautiful!you are kind! you are smart!";
    2. Scanner scanner = new Scanner(s);
    3. scanner.useDelimiter("!");
    4. while (scanner.hasNext())
    5. System.out.println(scanner.next());

    结果:

     

  • 相关阅读:
    L2-021 点赞狂魔(Python3)
    【深入理解C++】可调用对象、std::function、std::bind()
    机器学习集成学习进阶Xgboost算法原理
    华为VS苹果,你更pick谁?
    (附源码)ssm在线学习网站 毕业设计 080833
    深度 | 联手美邸、招商观颐等多家康养巨头,头部AI和机器人公司如何构建养老产业的智能新生态?
    mysql5.7.35安装配置教程【超级详细安装教程】
    英伟达RTX4090又火了?这次是真的着火了
    虚拟机设置固定ip
    智能优化与机器学习结合算法实现数据分类matlab代码清单
  • 原文地址:https://blog.csdn.net/qq_40408443/article/details/125540542