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

質問編集履歴

3

追記しました。

2021/08/20 11:15

投稿

showggg
showggg

スコア23

title CHANGED
File without changes
body CHANGED
@@ -15,19 +15,34 @@
15
15
  ### エラーが起きているコード
16
16
 
17
17
  ```Java
18
+ package first;
19
+
20
+ import java.text.MessageFormat;
21
+
22
+ public class Main {
23
+
24
+ public static void main(String[] args) {
25
+ // TODO 自動生成されたメソッド・スタブ
26
+ // ================================================================================
27
+ // MAIN
28
+ // ================================================================================
29
+
18
- 70 | String time = "2016.8.2";
30
+ String time = "2016.8.2";
19
- 71 | String[] textlist = time.split(".");
31
+ String[] textlist = time.split(".");
20
- 72!| String year = textlist[0];
32
+ String year = textlist[0];
21
- 73 | String month = String.format("%2s", textlist[1]).replace(" ", "0");
33
+     String month = String.format("%2s", textlist[1]).replace(" ", "0");
22
- 74 | String date = String.format("%2s", textlist[2]).replace(" ", "0");
34
+     String date = String.format("%2s", textlist[2]).replace(" ", "0");
23
- 75 | String text = MessageFormat.format("{0}{1}{2}", year, month, date);
35
+     String text = MessageFormat.format("{0}{1}{2}", year, month, date);
36
+ }
37
+
38
+ }
24
39
  ```
25
40
 
26
41
  ### エラー内容
27
42
 
28
43
  ```shell
29
44
  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
30
- at java_lesson/first.Main.main(Main.java:72)
45
+ at java_lesson/first.Main.main(Main.java:15)
31
46
  ```
32
47
 
33
48
  ### 備考

2

追記しました。

2021/08/20 11:15

投稿

showggg
showggg

スコア23

title CHANGED
File without changes
body CHANGED
@@ -30,6 +30,10 @@
30
30
  at java_lesson/first.Main.main(Main.java:72)
31
31
  ```
32
32
 
33
+ ### 備考
34
+
35
+ - textlist配列に無い要素を取り出そうとしていると怒られているので、原因はtime.split(".")が正常に値を返してないと思います。
36
+
33
37
  ### 環境
34
38
 
35
39
  Windows 10 Home

1

追記しました。

2021/08/20 11:11

投稿

showggg
showggg

スコア23

title CHANGED
File without changes
body CHANGED
@@ -8,6 +8,10 @@
8
8
  補足が必要な場合は追記いたします。
9
9
  よろしくお願いいたします。
10
10
 
11
+ ### やりたいこと
12
+
13
+ - 文字列「2016.8.2」を「20160802」と整形したい。
14
+
11
15
  ### エラーが起きているコード
12
16
 
13
17
  ```Java
@@ -16,6 +20,7 @@
16
20
  72!| String year = textlist[0];
17
21
  73 | String month = String.format("%2s", textlist[1]).replace(" ", "0");
18
22
  74 | String date = String.format("%2s", textlist[2]).replace(" ", "0");
23
+ 75 | String text = MessageFormat.format("{0}{1}{2}", year, month, date);
19
24
  ```
20
25
 
21
26
  ### エラー内容