Hey guys! So I'm working on a program that accepts 7 user inputs then stores them in an ArrayList. Then it replaces elements that start with the letter "B" with an aterisk. I'm getting a IndexOutOfBoundsException. Here's the code I have: `

package arraylist1;
import java.util.*;
public class ArrayList1 {

    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       ArrayList<String> list = new ArrayList<String>();

       String str = "";

       for(int i=0;i<7;i++)
          str = sc.next();
          list.add(str);

       for(int j=0; j<7;j++)
           if(list.get(j).startsWith("B")==true)
               list.set(j, " * ");

       System.out.print(list);
    }
}

`

You chose not to tell us the exact complete error message, including the relevant line numbers, but I'll give you this one for free...

Your indentation of line 13 shows what you intended, but it's not correct. Your for loop just contains the statement on line 12.

ps: The ==true on line 16 is a redundant no-op. It just "converts" true to true and false to false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.