0

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);
    }
}

`

2
Contributors
1
Reply
21
Views
2 Years
Discussion Span
Last Post by JamesCherrill
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.