1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

java 標準入力 (記録)

Posted at

##N行のデータの入力

    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      String[]input_line = new String[sc.nextInt()];
      sc.nextLine();
      for(int i=0;i<input_line.length;i++){
        input_line[i] = sc.nextLine();

        System.out.println(input_line[i]);
      }
    }

入力例1
3
aaaaa
bbbbbb
cccc

出力例1
aaaaa
bbbbbb
cccc


###3つのデータの入力

    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      String a = sc.next();
      String b = sc.next();
      String c = sc.next();
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);
  }

scにまず新しく入力されたものを入れるよと、new Scanner(System.in)と書く。
次に、sc.next()でスキャン文字を入れていく。nextの回数分scに入れる。


###N個のデータの入力 (空白除去)

  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String N = sc.nextLine();
    int n = Integer.parseInt(N);
    // String[] str = sc.nextLine().split(",");
    String[] str = sc.nextLine().split("[ ]");
    for(int i =0; i <n;i++){
      System.out.println(str[i]);
    }
  }

.split("[ ]")で空白行を除去して出力したりできます。

入力例1
3
aaaaa bbbbbb cccc

出力例1
aaaaa
bbbbbb
cccc


###カンマ区切りの3つのデータの入力

  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String[] str = sc.nextLine().split(",");
    System.out.println(str[0]);
    System.out.println(str[1]);
    System.out.println(str[2]);
  }
}

入力例1
aaaaa,bbbbbb,cccc

出力例1
aaaaa
bbbbbb
cccc


###カンマ区切りのN個のデータの入力

  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String N = sc.nextLine();
    int n = Integer.parseInt(N);
    String[] str = sc.nextLine().split(",");
    for(int i =0; i <n;i++){
      System.out.println(str[i]);
    }
  }

入力例1
3
aaaaa,bbbbbb,cccc

出力例1
aaaaa
bbbbbb
cccc

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?