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

質問編集履歴

1

エラーの修正

2020/07/08 06:04

投稿

CarpeDiemnosiru
CarpeDiemnosiru

スコア4

title CHANGED
@@ -1,1 +1,1 @@
1
- NullPointerExceptionの解決方法を教えてください。
1
+ ArrayIndexOutOfBoundsExceptionの解決方法を教えてください。
body CHANGED
@@ -8,8 +8,9 @@
8
8
  ### 発生している問題・エラーメッセージ
9
9
 
10
10
  ```
11
- Exception in thread "main" java.lang.NullPointerException
11
+ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
12
- at hello.Seiseki1.main(Seiseki1.java:12)
12
+ at hello.Seiseki1.main(Seiseki1.java:14)
13
+
13
14
  ```
14
15
 
15
16
  ### 該当のソースコード
@@ -19,41 +20,44 @@
19
20
 
20
21
  public class Seiseki1 {
21
22
 
22
- private static String[] names;
23
+
23
- private static double[][] score;
24
-
25
24
  public static void main(String[] args) {
25
+ int i;
26
26
  int total = 0;
27
+ int n = args.length/2;
28
+ String names[] = new String[n];
29
+ double score[][] = new double[n][2];
27
30
 
28
- for(int i = 0; i < args.length; i += 2 ) {
31
+ for(i = 0; i <args.length; i += 2 ) {
29
32
  names[i] = args[i];
30
33
  score[i][0] = Double.parseDouble(args[i + 1]);
31
34
  total += score[i][0];
32
-
35
+ }
33
- double average = (total/i);
36
+ double average = (total/(args.length/2));
34
37
  double ssum = 0.0;
35
38
  ssum += sqr(score[i][0] - average);
36
- double variance = ssum / i;
39
+ double variance = ssum / (args.length/2);
37
40
  double sd = Math.sqrt(variance);
38
41
  double hensachi = 50 + (score[i][0] - average)/sd * 10;
42
+
43
+
44
+ System.out.println("Average = " + average);
45
+ System.out.println("Standard Deviation = " + sd);
39
46
 
40
-
41
- for(i = 0; i < score.length; i++) {
47
+ for(i = 0; i <score.length; i++) {
42
48
  score[i][1] = hensachi;
43
49
 
44
- System.out.println("Average = " + average);
45
- System.out.println("Standard Deviation = " + sd);
46
50
 
47
-
48
- System.out.println(" " + names[i]);
51
+ System.out.print(" " + names[i] + " ");
52
+ System.out.print(score[i][0] + " ");
49
- System.out.println(score[i][0]);
53
+ System.out.println(hensachi);
50
- System.out.println(score[i][1]);
51
54
  }
52
55
 
56
+
53
57
  }
54
58
 
55
- }
56
59
 
60
+
57
61
  private static double sqr(double d) {
58
62
  return d * d;
59
63
  }
@@ -63,7 +67,7 @@
63
67
 
64
68
  ### 試したこと
65
69
 
66
- 配列nullで初期化したりしたのですが、うまくいきません。
70
+ forの条件式変えたりしたのですが、うまくいきません。
67
71
 
68
72
  ### 補足情報
69
73