You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: error_templates/java/test_files/array_required_type_error/test.txt
+6-17Lines changed: 6 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -8,33 +8,22 @@ NotArray.java:4: error: array required, but int found
8
8
template: "Java.ArrayRequiredTypeError"
9
9
---
10
10
# ArrayRequiredTypeError
11
-
This error occurs because the variable `number` is declared as an `int` rather than an array. You're attempting to access an index (`[0]`) on a variable that's not an array.
11
+
This error occurs because the variable `number` is declared as an `int` rather than an array. You're attempting to access an index (`0`) on a variable that's not an array.
12
12
```
13
13
int number = 5;
14
14
int value = number[0];
15
-
^
15
+
^
16
16
}
17
17
}
18
18
```
19
19
## Steps to fix
20
-
### 1. Change variable type to an array
20
+
### Convert variable to an array
21
+
Declare the variable `number` as an array of `int`.
21
22
```diff
22
23
public class NotArray {
23
24
public static void main(String[] args) {
24
-
- int number = 5;
25
-
+ int[] number = {5};
25
+
- int number = 5;
26
+
+ int[] number = {5};
26
27
int value = number[0];
27
28
}
28
-
}
29
-
```
30
-
### 2. Initialize an array and access its index
31
-
```diff
32
-
public class NotArray {
33
-
public static void main(String[] args) {
34
-
int[] number = {5};
35
-
+ number[0] = 5;
36
-
int value = number[0];
37
-
}
38
-
}
39
29
```
40
-
These changes will rectify the error by ensuring the variable is treated as an array when accessing elements by index.
0 commit comments