zoukankan      html  css  js  c++  java
  • LY.JAVA.DAY12.Scanner

    2018-07-24 13:23:18

    Scanner类

    一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器

     1 package cn.itcast_01;
     2 
     3 /*
     4  * Scanner:用于接收键盘录入数据。
     5  * 
     6  * 前面的时候:
     7  *         A:导包
     8  *         B:创建对象
     9  *         C:调用方法
    10  * 
    11  * System类下有一个静态的字段:
    12  *         public static final InputStream in; 标准的输入流,对应着键盘录入。
    13  * 
    14  *         InputStream is = System.in;
    15  * 
    16  * class Demo {
    17  *         public static final int x = 10;
    18  *         public static final Student s = new Student();
    19  * }
    20  * int y = Demo.x;
    21  * Student s = Demo.s;
    22  * 
    23  * 
    24  * 构造方法:
    25  *         Scanner(InputStream source)
    26  */
    27 import java.util.Scanner;
    28 
    29 public class ScannerDemo {
    30     public static void main(String[] args) {
    31         // 创建对象
    32         Scanner sc = new Scanner(System.in);
    33 
    34         int x = sc.nextInt();
    35         
    36         System.out.println("x:" + x);
    37     }
    38 }
     1 package cn.itcast_02;
     2 
     3 import java.util.Scanner;
     4 
     5 /*
     6  * 基本格式:
     7  *         public boolean hasNextXxx():判断是否是某种类型的元素
     8  *         public Xxx nextXxx():获取该元素
     9  * 
    10  * 举例:用int类型的方法举例
    11  *         public boolean hasNextInt()
    12  *         public int nextInt()
    13  * 
    14  * 注意:
    15  *         InputMismatchException:输入的和你想要的不匹配
    16  */
    17 public class ScannerDemo {
    18     public static void main(String[] args) {
    19         // 创建对象
    20         Scanner sc = new Scanner(System.in);
    21 
    22         // 获取数据
    23         if (sc.hasNextInt()) {
    24             int x = sc.nextInt();
    25             System.out.println("x:" + x);
    26         } else {
    27             System.out.println("你输入的数据有误");
    28         }
    29     }
    30 }

     常用的两个方法

    public  int  nextInt(); 获取一个int类型的值

    public  String  nextLine();获取一个String类型的值

    先获取一个数值,后获取一个字符串会出现小问题

    主要原因就是,那个换行符号的问题【Enter】

    解决方案:

      A:先获取一个数值后,再创建一个新的键盘录入对象获取字符串。

      B:把所有的数据按照字符串获取,然后要什么就对应转换为什么

     1 package cn.itcast_03;
     2 
     3 import java.util.Scanner;
     4 
     5 /*
     6  * 常用的两个方法:
     7  *         public int nextInt():获取一个int类型的值
     8  *         public String nextLine():获取一个String类型的值
     9  * 
    10  * 出现问题了:
    11  *         先获取一个数值,在获取一个字符串,会出现问题。
    12  *         主要原因:就是那个换行符号的问题。
    13  * 如何解决呢?
    14  *         A:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
    15  *         B:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。
    16  */
    17 public class ScannerDemo {
    18     public static void main(String[] args) {
    19         // 创建对象
    20         Scanner sc = new Scanner(System.in);
    21 
    22         // 获取两个int类型的值
    23         // int a = sc.nextInt();
    24         // int b = sc.nextInt();
    25         // System.out.println("a:" + a + ",b:" + b);
    26         // System.out.println("-------------------");
    27 
    28         // 获取两个String类型的值
    29         // String s1 = sc.nextLine();
    30         // String s2 = sc.nextLine();
    31         // System.out.println("s1:" + s1 + ",s2:" + s2);
    32         // System.out.println("-------------------");
    33 
    34         // 先获取一个字符串,在获取一个int值
    35         // String s1 = sc.nextLine();
    36         // int b = sc.nextInt();
    37         // System.out.println("s1:" + s1 + ",b:" + b);
    38         // System.out.println("-------------------");
    39 
    40         // 先获取一个int值,在获取一个字符串
    41         // int a = sc.nextInt();
    42         // String s2 = sc.nextLine();
    43         // System.out.println("a:" + a + ",s2:" + s2);
    44         // System.out.println("-------------------");
    45 
    46         int a = sc.nextInt();
    47         Scanner sc2 = new Scanner(System.in);
    48         String s = sc2.nextLine();
    49         System.out.println("a:" + a + ",s:" + s);
    50     }
    51 }
    年轻人能为世界年轻人能为世界做些什么
  • 相关阅读:
    南大《软件分析》课程笔记——Intermediate Representation
    leetcode刷题
    shiro550反序列化分析
    南大《软件分析》课程笔记——第一课
    再见2020
    S2-002漏洞分析
    编码&解码工具
    正码、反码和补码
    Java数据结构(二)
    Java数据集合
  • 原文地址:https://www.cnblogs.com/twinkle-star/p/9356684.html
Copyright © 2011-2022 走看看