static PrintStream err 「標準」エラー出力ストリームです。 static InputStream in 「標準」入力ストリームです。 static PrintStream out 「標準」出力ストリームです。
< 標準入力 > 標準出力 2> 標準エラー出力
|
- import java.io.*;
- public class Sysin2Sysout {
- public static void main (String[] args) {
- int b;
- try {
- while ((b = System.in.read()) != -1) {
- System.out.println("char = " + (char)b + ", hex = " +
- Integer.toHexString(b));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
□ 実行結果
$ java Sysin2Sysout Hello <-- 入力したデータ char = H, hex = 48 char = e, hex = 65 char = l, hex = 6c char = l, hex = 6c char = o, hex = 6f , hex = d char = , hex = a あいうえお <-- 入力したデータ char = ?, hex = 82 char = ?, hex = a0 char = ?, hex = 82 char = ¢, hex = a2 char = ?, hex = 82 char = ?, hex = a4 char = ?, hex = 82 char = ?, hex = a6 char = ?, hex = 82 char = ¨, hex = a8 , hex = d char = , hex = a
- import java.io.*;
- public class Sysin2SysoutWithJp {
- public static void main (String[] args) {
- BufferedReader sysin
- = new BufferedReader(new InputStreamReader(System.in));
- String str = null;
- try {
- while ((str = sysin.readLine()) != null) {
- System.out.println(str);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
□ 実行結果
$ java Sysin2Sysout 入力データ <-- 入力したデータ 入力データ <-- 出力されたデータ 出力データ <-- 入力したデータ 出力データ <-- 出力されたデータ
□ 実行結果
$ java Sysin2Sysout02 Hello out: Hello err: Hello Java out: Java err: Java World! out: World! err: World!
- import java.io.*;
- public class Sum11 {
- public static void main (String[] args) {
- BufferedReader sysin = /* コードを記述してください。*/ ;
- String str = null;
- int sum = 0;
- try {
- while (/* コードを記述してください。*/) {
- if (/* コードを記述してください。*/) {
- break;
- }
- // コードを記述してください。
- }
- } catch (IOException e) {
- System.out.println("IOException");
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("sum = " + sum);
- }
- }
□ 実行結果
$ java Sum11 22 44 66 -99 q sum = 33
- import java.io.*;
- public class Sum12 {
- public static void main (String[] args) {
- BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
- String str = null;
- int sum = 0;
- try {
- // コードを記述してください。
- } catch (IOException e) {
- System.err.println("IOException");
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("sum = " + sum);
- }
- }
□ 実行結果
$ java Sum12 22 44 rr NumberFormatException for input rr 66 ii NumberFormatException for input ii -99 qq NumberFormatException for input qq q sum = 33
- import java.io.*;
- public class Grep01 {
- public static void main (String[] args) {
- BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
- String str = null;
- try {
- // コードを記述してください。
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
□ 実行結果
$ cat Grep01.java | java Grep01 str String str = null; while ((str = sysin.readLine()) != null) { if (str.matches(".*" + args[0] + ".*")) { System.out.println(str);
- import java.io.*;
- public class Janken01 {
- static BufferedReader br = null;
- public static void main(String[] args) {
- int draw = 0;
- int win = 0;
- int lose = 0;
- try {
- int user = -1;
- br = new BufferedReader(new InputStreamReader(System.in));
- while ((user = read()) != -1) {
- int computer = (int)(Math.random() * 3);
- // コードを記述してください。
- System.err.println(win + " 勝 " + lose + " 敗 " + draw + " 分 ");
- }
- System.out.println();
- System.out.println(win + " 勝 " + lose + " 敗 " + draw + " 分 ");
- }
- catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- } catch (Exception e) {
- }
- }
- }
- static int read() throws IOException {
- int user = -1;
- while ((user = read("グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> ")) != -1) {
- if (user >= 0 && user <= 3) {
- break;
- }
- }
- return user;
- }
- static int read(String message) throws IOException {
- System.out.print(message);
- String str = br.readLine();
- if (str == null) {
- return -1;
- }
- int user = -1;
- // コードを記述してください。
- }
- }
□ 実行結果
$ java Janken01 グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> g あなたの勝 1 勝 0 敗 0 分 グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> g あなたの敗け 1 勝 1 敗 0 分 グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> c 引き分け 1 勝 1 敗 1 分 グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> c 引き分け 1 勝 1 敗 2 分 グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> p あなたの勝 2 勝 1 敗 2 分 グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> p あなたの勝 3 勝 1 敗 2 分 グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> d グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> x 3 勝 1 敗 2 分
- import java.io.*;
- public class NumberGuess01 {
- private static BufferedReader br = null;
- public static void main(String[] args) {
- int computer = getNumber();
- System.err.println("computer = " + computer);
- int[] n = new int[2];
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- int user;
- int count = 0;
- while ((user = read()) != -1) {
- n = checkNumbers(user, computer);
- if (user == computer) {
- System.out.println("ビンゴ! " + count);
- break;
- }
- System.out.println("桁違いの同じ数 = " + n[0]);
- System.out.println(" 一致した数 = " + n[1]);
- System.out.println();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- } catch (Exception e) {
- }
- }
- }
- static int[] checkNumbers(int u, int c) {
- // コードを記述してください。
- return n;
- }
- static int getNumber() {
- // コードを記述してください。
- }
- static int read() throws IOException {
- int user = -1;
- while ((user = read("2桁-3桁の正整数をを入力してください。==> ")) != -1) {
- if (user >= 10 && user <= 999) {
- break;
- }
- }
- return user;
- }
- static int read(String message) throws IOException {
- System.out.print(message);
- String str = br.readLine();
- if (str == null) {
- return 0;
- }
- int user = 0;
- try {
- user = Integer.parseInt(str);
- } catch (NumberFormatException e) {
- }
- return user;
- }
- }
□ 実行結果
$ java NumberGuess01 2> null 2桁-3桁の正整数をを入力してください。==> 123 桁違いの同じ数 = 1 一致した数 = 0 2桁-3桁の正整数をを入力してください。==> 124 桁違いの同じ数 = 0 一致した数 = 0 2桁-3桁の正整数をを入力してください。==> 345 桁違いの同じ数 = 0 一致した数 = 1 2桁-3桁の正整数をを入力してください。==> 367 桁違いの同じ数 = 1 一致した数 = 1 2桁-3桁の正整数をを入力してください。==> 378 桁違いの同じ数 = 0 一致した数 = 2 2桁-3桁の正整数をを入力してください。==> 379 ビンゴ! 6
- // 1602
- // package application;
- // import product.*;
- import java.util.*;
- import java.io.*;
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- TreeMap p = new TreeMap();
- SetProduct s = new SetProduct("SA01", "パソコン・セット", 10, p);
- System.out.println("セット商品のインスタンスを生成しました。 s = " + s);
- System.out.println();
- AdminProduct ap = new AdminProduct();
- ap.read(s, args[0]);
- System.out.println("商品のデータを入力しました。 p = " + p);
- System.out.println("セット商品のデータを設定しました。 s = " + s);
- BufferedReader br = null;
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- String str = null;
- while ((str = br.readLine()) != null && !str.toUpperCase().equals("X")) {
- System.out.println("商品コード " + str + " の商品は " + s.query(str) + " です。");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- ap.write(s, args[1]);
- System.out.println("セット商品のデータをファイルに書き出しました。 s = " + s);
- }
- }
A A001 パソコン 188000 A P001 プリンター 24000 A C001 CD-ROM 6500 A M001 USBメモリー 9800 A H001 ハードディスク 9200 U C001 DVD-ROM 9200 D M001
□ 実行結果
$ java -cp classes application.TestProduct products.txt out.txt セット商品のインスタンスを生成しました。 s = [SA01, パソコン・セット, 10, 0] 商品のデータを入力しました。 p = {A001=[A001, パソコン, 188000], C001=[C001, DVD -ROM, 9200], H001=[H001, ハードディスク, 9200], P001=[P001, プリンター, 24000]} セット商品のデータを設定しました。 s = [SA01, パソコン・セット, 10, 207360] A001 商品コード A001 の商品は [A001, パソコン, 188000] です。 B001 商品コード B001 の商品は [Not Found] です。 C001 商品コード C001 の商品は [C001, DVD-ROM, 9200] です。
A001 パソコン 188000 C001 DVD-ROM 9200 H001 ハードディスク 9200 P001 プリンター 24000