I'm trying to take user input and put it into a character array and then print it out.... It's part of a bigger program and since i'm a new coder, I was hoping if you could keep the program simple without.... I get the error "Array index out of bounds". I tried changing the length of the array but that still didn't work.
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] ToEdit = new char [];
Scanner sc = new Scanner(System.in);
for (int i=0; i<5; i++)
{
System.out.println(i + ":");
ToEdit[i] = sc.next().charAt(i);
}
System.out.println(ToEdit);
}
Thank you
char[] ToEdit = new char [];doesn't compile, hence you can't run your code , and you won't get an exception. Once you do pass a length in thenew char[1234], then you can change the upper bound of the loop to reflect the length of the array:for (int i=0; i<ToEdit.length; i++)