473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ArrayIndexOutOfBoundsException occurred

When I tried to run a code, an error occurred. Please explain why error occurred and how to fix it, thanks.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. /* package whatever; // don't place package name! */
  2.  
  3. /* Name of the class has to be "Main" only if the class is public. */
  4. import java.util.ArrayList;
  5. class Demo
  6. {
  7.     public static void main(String[] args) {
  8.         ArrayList<String> list1 = new ArrayList<String>();
  9.         ArrayList<String> list2 = new ArrayList<String>();
  10.         int index = 0;
  11.         list1.add("ahio");
  12.         list1.add("bro");
  13.         list1.add("higoodbye");
  14.         list2.add("hi");
  15.         list2.add("goodbye");
  16.         for (String x : list1) {
  17.             for (String y : list2) {
  18.                 if (x.contains(y)) {
  19.                     index = list1.indexOf(x);
  20.                     list1.set(index, x.replaceAll(y, y.replaceAll("[a-zA-Z]", "*")));
  21.                 }
  22.             }
  23.         }
  24.         System.out.println(String.join(" ", list1));
  25.     }
  26. }
  27.  
Here is my error:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Warning\Error
  3.  
  4. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
  5.     at java.util.ArrayList.elementData(ArrayList.java:418)
  6.     at java.util.ArrayList.set(ArrayList.java:446)
  7.     at Demo.main(Main.java:20)
  8.  
May 1 '17 #1
2 1176
chaarmann
785 Expert 512MB
Line 19 will return -1 if the sting x cannot be found in list1.
You are not checking this and trying to access an element with index "-1" in line 20.

You may argue that the String should always be found because you are looping through all strings in the outer for-loop. But that is not true because you are changing the string in the list while looping. As an effect the "x" contains the original string from the list while the list contains the changed one.
May 2 '17 #2
you are use array index more than own array at line 20
May 3 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
2
by: David Stevenson | last post by:
Programs from: Sheng Liang, The Java Native Interface, Programmer's Guide and Specification, The Java Series, (c) 1999, pp. 38-39. I thought I copied the program pretty exactly, but I don't know...
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
31
by: ivannavi | last post by:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String Temp = jComboBox1.getSelectedItem().toString(); ...
22
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to...
1
by: al666940 | last post by:
Hello everyone, I'm coding card functions (shuffle, draw, etc) using the Vector class and functions, and I'm almost finished except for an exception that I can't seem to get rid of. Can anyone...
1
by: shaikhussain | last post by:
public class EmployeeServicesTestCase { /** * @param args */ public static void main(String s)throws Exception { // TODO Auto-generated method stub BeanFactory beans=new...
2
by: David Rothabuer | last post by:
I work with an integration engine that is java based. All my scripting is javascripting and I've run into a problem that I can't find a solution to. I am getting this error when I attempt to read...
1
by: Buena Velasco | last post by:
I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOfBounds... " means but I couldn't tell which part in the looping it is or in other. import...
1
by: JanineXD | last post by:
I was browsing the web for examples on how to partition in Java. I've got to this website that has a detailed example about Partitioning so I've tried to run it on my JCreator Pro. I've only got...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.