Java Scanner類の用法及びnextLine()による改行問題の実例分析


本論文の実例は、Java Scanner類の用法およびnextLine()によって生成される改行符の問題を述べている。皆さんに参考にしてあげます。具体的には以下の通りです。
解析理解:Scanner sc = new Scanner(System.in);

package cn.itcast_01;
/*
 * Scanner:          。
 *
 *      :
 *     A:  
 *     B:    
 *     C:    
 *
 *     :Scanner sc = new Scanner(System.in);
 * System          :
 *     public static final InputStream in;       ,       。
 *
 *     InputStream is = System.in;
 *
 * class Demo {
 *     public static final int x = 10;
 *     public static final Student s = new Student();
 * }
 * int y = Demo.x;
 * Student s = Demo.s;
 *
 *
 *     :
 *     Scanner(InputStream source)
 */
import java.util.Scanner;
public class ScannerDemo {
  public static void main(String[] args) {
    //     
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    System.out.println("x:" + x);
  }
}

Scanner類のhasNextInt()nextInt()の方法

package cn.itcast_02;
import java.util.Scanner;
/*
 *     :
 *     public boolean hasNextXxx():            
 *     public Xxx nextXxx():     
 *
 *   : int       
 *     public boolean hasNextInt()
 *     public int nextInt()
 *
 *   :
 *     InputMismatchException:           
 */
public class ScannerDemo {
  public static void main(String[] args) {
    //     
    Scanner sc = new Scanner(System.in);
    //     
    if (sc.hasNextInt()) {
      int x = sc.nextInt();
      System.out.println("x:" + x);
    } else {
      System.out.println("        ");
    }
  }
}

ScannerクラスのnextLine()による改行問題

package cn.itcast_03;
import java.util.Scanner;
/*
 *        :
 *     public int nextInt():    int    
 *     public String nextLine():    String    
 *
 *      :
 *            ,        ,     。
 *         :           。
 *      ?
 *     A:        ,                  。
 *     B:               ,     ,          。
 */
public class ScannerDemo {
  public static void main(String[] args) {
    //     
    Scanner sc = new Scanner(System.in);
    //     int    
    // int a = sc.nextInt();
    // int b = sc.nextInt();
    // System.out.println("a:" + a + ",b:" + b);
    // System.out.println("-------------------");
    //     String    
    // String s1 = sc.nextLine();
    // String s2 = sc.nextLine();
    // System.out.println("s1:" + s1 + ",s2:" + s2);
    // System.out.println("-------------------");
    //         ,     int 
    // String s1 = sc.nextLine();
    // int b = sc.nextInt();
    // System.out.println("s1:" + s1 + ",b:" + b);
    // System.out.println("-------------------");
    //      int ,        ,      
    // int a = sc.nextInt();
    // String s2 = sc.nextLine();
    // System.out.println("a:" + a + ",s2:" + s2);
    // System.out.println("-------------------");
    int a = sc.nextInt();
    Scanner sc2 = new Scanner(System.in);
    String s = sc2.nextLine();
    System.out.println("a:" + a + ",s:" + s);
  }
}

java関連の内容についてもっと興味がある読者は、当駅のテーマを調べてもいいです。「Javaファイルとディレクトリの操作テクニックのまとめ」、「Javaデータ構造とアルゴリズム教程」、「Java操作DOMノード技術のまとめ」、「Javaキャッシュ操作テクニックのまとめ
本論文で述べたように、皆さんのjavaプログラムの設計に役に立ちます。