teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

試したことを追記しました。

2020/04/05 00:40

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -88,6 +88,11 @@
88
88
  java.lang.NullPointerException
89
89
  原因:Ctr+Zを押して入力終了をさせたら上記のエア-がかえった。
90
90
  ### 試したこと
91
+ while (true) {
92
+ str = br.readLine();
93
+ if (str == null || str.isEmpty()) break;
94
+ にすると、今度は複数の値を読み込まれません。
95
+
91
- while (!(str = br.readLine()).isEmpty()){
96
+ while ((str = br.readLine()) != null && !str.isEmpty());
97
+
92
- While((str = br.readLine())!= null){
98
+ ⇒こっちは、a入れても例外処理されないし、ctr+Z押して終了させると、やはりNullPointerExceptionが発生しますね
93
- にしてみましたがダメでした。

2

追記しました。

2020/04/05 00:40

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -7,34 +7,83 @@
7
7
  <分からないこと>
8
8
  文字列を数値に変換して前回の数値と今回入力された数値の積を出す記述方法や
9
9
  ※入力はscannar以外を使用して必ずIOExceptionを発生させる
10
+ <仕様>
11
+ ・数値以外が入力された場合は、「数値を入力してください」を表示する
12
+ ・入出力例外が発生した場合は、「入出力例外が発生しました」を表示して、処理を終了する
13
+ ・改行のみが入力された時点で入力を終了する
14
+ ・演算の結果として、「入力された値の積 :(演算結果)」を表示する
15
+ ・積算は「*」オペレータを使用し、double型の精度まで表示する
16
+ ・演算結果は、3桁カンマ編集して表示する
17
+ ・入力が1つもない場合の演算結果は「1」とする
10
18
 
19
+ ■ 出力例
20
+ 積を求める数値を入力してください
21
+ 13
22
+ 9.3
23
+ a
24
+ 数値を入力してください
25
+ 32
26
+ 6.7
27
+
28
+ 入力された値の積 : 25,920.96
11
29
  ### 該当のソースコード
12
30
 
13
31
  言語名JAVA
14
32
 
33
+ import java.io.*;
34
+
35
+ public class Exercise3 {
36
+
15
37
  public static void main(String[] args) {
38
+ // TODO Auto-generated method stub
39
+ int x;
16
- System.out.println("積を求める数値を入力してくだ");
40
+ System.out.println("入力された値の積 ::");
41
+ //keyboard inputs
42
+ InputStreamReader isr = new InputStreamReader(System.in);
43
+ BufferedReader br = new BufferedReader(isr);
44
+
45
+
17
46
  try {
18
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//入力
19
- double product = 1;//initial value
20
- String str;
47
+ String buf;
48
+ while(true) {
49
+ buf = br.readLine();//Arise an IOException intentionally
50
+ if(buf.contentEquals("\n"))//if the value is the only new line , the input wraps up
51
+ break;
52
+ //if it is a blank, the result is "1"
53
+ if(buf.contentEquals(""))
54
+
55
+
56
+ System.out.println(buf);
57
+ }
58
+ try {//Exception handling
59
+ x = Integer.parseInt(buf);//arise an Exception if input value wasn't number
60
+
61
+ }catch (NumberFormatException e) {
62
+ x = 0;
63
+ System.out.println("数値を入力してください");
64
+ }
65
+
66
+
67
+
68
+ }catch(IOException e) {//handle the exception and print line the stings below
69
+ System.out.println("入出力例外が発生しました");
70
+ }
71
+
72
+
73
+
74
+
75
+
76
+ }
77
+
78
+ }
79
+ 追記:
80
+ 現在のソースコード:
21
- while (!(str = br.readLine()).isEmpty())//入力された値が空なら入力終了
81
+ while (!(str = br.readLine()).isEmpty())//入力された値が空なら入力終了
22
82
  try {
23
83
  product *= Double.valueOf(str);//受け取った値を数値(Double type)に変換して変数"product"に代入
24
84
  } catch (NumberFormatException e) {//数値以外の入力なら例外発生
25
85
  System.out.println("数値を入力してください");//その例外が発生したら出力
26
86
  }
27
- str = String.format("%,.16g", product);//積算された値をString.format methodで編集してカンマ区切り
28
- if (str.indexOf('.') >= 0)//search index of "."
29
- str = str.replaceAll("0*$", "").replaceAll("\.$", "");
30
- System.out.println("入力された値の積 : " + str);
31
- } catch (IOException e) {
32
- System.out.println("入出力例外が発生しました");
33
- }
34
- }
35
-
36
-
37
- }
38
87
  ### 発生している問題・エラーメッセージ
39
88
  java.lang.NullPointerException
40
89
  原因:Ctr+Zを押して入力終了をさせたら上記のエア-がかえった。

1

現在のソースコードと例外発生の追加

2020/04/05 00:08

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -6,87 +6,39 @@
6
6
  Java でコンソールから複数の数値(整数・実数)を受け取って、入力された数値の積を表示する
7
7
  <分からないこと>
8
8
  文字列を数値に変換して前回の数値と今回入力された数値の積を出す記述方法や
9
- 入力が1つもない場合の演算結果を1に戻るとことかチンプンカンプンです。
10
- 仕様通りに書けなくて困っています。
11
- 仕様は以下の通りです。
12
- <仕様>
13
- ・数値以外が入力された場合は、「数値を入力してください」を表示する
14
- ・入出力例外が発生した場合は、「入出力例外が発生しました」を表示して、処理を終了する
15
- ・改行のみが入力された時点で入力を終了する
16
- ・演算の結果として、「入力された値の積 :(演算結果)」を表示する
17
- ・積算は「*」オペレータを使用し、double型の精度まで表示する
18
- ・演算結果は、3桁カンマ編集して表示する
19
- ・入力が1つもない場合の演算結果は「1」とする
20
-
21
9
  ※入力はscannar以外を使用して必ずIOExceptionを発生させる
22
10
 
23
- ■ 出力例
24
- 積を求める数値を入力してください
25
- 13
26
- 9.3
27
- a
28
- 数値を入力してください
29
- 32
30
- 6.7
31
-
32
- 入力された値の積 : 25,920.96
33
-
34
-
35
-
36
11
  ### 該当のソースコード
37
12
 
38
13
  言語名JAVA
39
- import java.io.*;
40
14
 
41
- public class Exercise3 {
42
-
43
15
  public static void main(String[] args) {
44
- // TODO Auto-generated method stub
45
- int x;
46
- System.out.println("入力された値の積 ::");
16
+ System.out.println("積を求める数値を入力してくだ");
47
- //keyboard inputs
48
- InputStreamReader isr = new InputStreamReader(System.in);
49
- BufferedReader br = new BufferedReader(isr);
50
-
51
-
52
17
  try {
18
+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//入力
19
+ double product = 1;//initial value
53
- String buf;
20
+ String str;
54
- while(true) {
55
- buf = br.readLine();//Arise an IOException intentionally
21
+ while (!(str = br.readLine()).isEmpty())//入力された値が空なら入力終了
22
+ try {
56
- if(buf.contentEquals(""))//if the value is the only new line , the input wraps up
23
+ product *= Double.valueOf(str);//受け取った値を数値(Double type)に変換して変数"product"に代入
57
- break;
24
+ } catch (NumberFormatException e) {//数値以外の入力なら例外発生
58
-
59
-
60
-
61
- System.out.println(buf);
25
+ System.out.println("数値を入力してください");//その例外が発生したら出力
62
- }
26
+ }
63
- try {//Exception handling
27
+ str = String.format("%,.16g", product);//積算された値をString.format methodで編集してカンマ区切り
64
- x = Integer.parseInt(buf);//arise an Exception if input value wasn't number
28
+ if (str.indexOf('.') >= 0)//search index of "."
65
-
29
+ str = str.replaceAll("0*$", "").replaceAll("\.$", "");
30
+ System.out.println("入力された値の積 : " + str);
66
- }catch (NumberFormatException e) {
31
+ } catch (IOException e) {
67
- x = 0;
68
- System.out.println("数値を入力してください");
32
+ System.out.println("入例外が発生ました");
69
- }
33
+ }
70
-
71
-
72
-
73
- }catch(IOException e) {//handle the exception and print line the stings below
74
- System.out.println("入出力例外が発生しました");
75
- }
34
+ }
76
-
77
-
78
-
79
-
80
-
81
- }
82
35
 
36
+
37
+ }
38
+ ### 発生している問題・エラーメッセージ
39
+ java.lang.NullPointerException
40
+ 原因:Ctr+Zを押して入力終了をさせたら上記のエア-がかえった。
83
41
  ### 試したこと
84
-
42
+ while (!(str = br.readLine()).isEmpty()){
85
- 入力された文字列数値に変換し、数値以外の入力→例外処理
43
+ While((str = br.readLine())!= null){
86
- IOExceptionを発生させて例外処理
87
-
88
-
89
- みません大至急お願います
44
+ にしてみましたダメでた。
90
-
91
-
92
-