Whenever I run my code, it says Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0. I make sure that my I value isn't exceeded but it still says it. Can you guys help me?
I made sure that nothing in my for loop exceeded my I value but it seems to be something else that is triggering the problem.
And by the way, sorry if my formatting is incorrect. This is my first time using stack overflow.
One more thing, my compiler says that the error is in line 17(inside my for loop).
Here's my code:
import java.io.*;
public class Main {
public static int length1;
public static String numbers;
public static String str;
public static void main(String[] args)throws IOException{
System.out.println("");
System.out.println("Hello world!");
char[] nums = new char[length1];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type in numbers with spaces in them.");
numbers = br.readLine();
System.out.println("");
for(int i = 0; i < numbers.length(); i++){
nums[i] = numbers.charAt(i);
System.out.println(numbers.charAt(i));
}
length1 = numbers.length();
}
}
length1atchar[] nums = new char[length1];?numsis a zero-length array becauselength1 = 0numis zero length. But you try to set the first element ofnum(i.e.num[0]) in the first iteration of theforloop.