I teach Java by myself (super beginner)

I checked the execution contents on the console,
The content I want matches
The error message does not disappear.

[Do not change the console contents and eliminate the error message]

I want the output below

str1 :he str2 :go
str1 :go str2 :so
str1 :so str2 :gr

The result I want to achieve on the console is
Because an error message has occurred
I want to solve only the error message without changing the output result

Error message (console content)
str1 :he str2 :go
str1 :go str2 :so
str1 :so str2 :gr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
    at Task4.main(Task4.java:10)
Applicable source code
public class Task4 {
    public static void main(String[] args){
        String[] list = {"hello","good","soso","great"};
        for(int i = 0;i<list.length;i++) {
            String str1 = list[i].substring(0,2);
            String str2 = list[i + 1].substring(0,2);
            System.out.println("str1 :" + str1 + "str2 :" +str2);
What I tried

I was suspicious about the following (list [i + 1]) on line 17, so I changed + and made a trial and error.

String str2 = list[i + 1].substring(0,2);
Supplemental information (FW/tool ​​version, etc.)

Development environment: Using Eclipse 20-06

  • Answer # 1

    The reason is that i+1 is trying to refer to a number larger than the number of arrays.

    ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
    Google Translate: ArrayIndexOutOfBoundsException: Index 4 is out of range of length 4
    How to read and deal with error messages, search and question principles

    As long as you try to reference the last data + 1, it will be outside the range of the index, so you can avoid it.

    The simple thing is to reduce the number of laps by one.

    -for(int i = 0;i

    After that, if list.length and i+1 match, it will be a loop break, or something like that.