Home>
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
Related articles
- java - i want to get the entire list from the db and display it, but an error occurs when executing the application
- java - i want to resolve an error that occurs when running a spring application
- java - i want to eliminate the error that occurs when starting an application that uses jsonic
- java - an error occurs when changing a dynamic web module from a static web module with sts (spring-tool-suite)
- java - i get an error when using an array when instantiating
- java - an error occurs when executing the jar file
- ruby - when posting an image, an error occurs where the image is not defined
- java - i get a syntax error on (s), misplaced construct (s) error what should i do? help me
- ios - no numbers are displayed and an error occurs
- linux - error occurs when firewall is stopped
- java create an array from arbitrary input
- typescript array error
- how to transition to a specific page when a 404 error occurs in window iis
- [php/xampp] an error occurs when setting the include path
- java - i want to display the corresponding argument in the error message when the command argument is not an integer
- java - i want to get a 404 error when entering a random url in spring boot
- an error occurs in latex \ begin {document}
- i'm getting a java compile error "no class, interface or enum", but i think the parentheses are supported
- vim - error occurs in tarexe
- [vba] i get an "element not found" error when web scraping with selenium
Trends
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
The reason is that i+1 is trying to refer to a number larger than the number of arrays.
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.
After that, if list.length and i+1 match, it will be a loop break, or something like that.