Reputation: 1
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();
}
}
Upvotes: 0
Views: 73
Reputation: 89
so your problem is at this line...
char[] nums = new char[length1];
length1
is does not have a value.
Try This...
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!");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type in numbers with spaces in them.");
numbers = br.readLine();
char[] nums = new char[numbers.length()];
System.out.println("");
for (int i = 0; i < numbers.length(); i++) {
nums[i] = numbers.charAt(i);
System.out.println(numbers.charAt(i));
}
}
}
Upvotes: 1