java.lang.ArrayIndexOutofBounds循环内部异常

java.lang.ArrayIndexOutofBounds循环内部异常,java,Java,每当我运行代码时,在线程“main”java.lang.ArrayIndexOutOfBoundsException:0中都会显示异常。我确保没有超过我的I值,但它仍然会显示它。你们能帮帮我吗 我确保for循环中没有任何内容超过我的I值,但似乎是其他原因触发了问题 顺便说一下,如果我的格式不正确,很抱歉。这是我第一次使用堆栈溢出 还有一件事,我的编译器说错误在第17行(在for循环中) 这是我的密码: import java.io.*; public class Main { publi

每当我运行代码时,在线程“main”java.lang.ArrayIndexOutOfBoundsException:0中都会显示
异常。我确保没有超过我的I值,但它仍然会显示它。你们能帮帮我吗

我确保for循环中没有任何内容超过我的I值,但似乎是其他原因触发了问题

顺便说一下,如果我的格式不正确,很抱歉。这是我第一次使用堆栈溢出

还有一件事,我的编译器说错误在第17行(在for循环中)

这是我的密码:

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();
    }
}
import java.io.*;
公共班机{
公共静态整数长度1;
公共静态字符串编号;
公共静态字符串str;
公共静态void main(字符串[]args)引发IOException{
System.out.println(“”);
System.out.println(“你好,世界!”);
char[]nums=新字符[length1];
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入带有空格的数字”);
数字=br.readLine();
System.out.println(“”);
对于(int i=0;i
所以你的问题就在这一行

char[]nums=新字符[length1]

length1
is没有值。 试试这个

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));
        }
    }
}
import java.io.*;
公共班机{
公共静态整数长度1;
公共静态字符串编号;
公共静态字符串str;
公共静态void main(字符串[]args)引发IOException{
System.out.println(“”);
System.out.println(“你好,世界!”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入带有空格的数字”);
数字=br.readLine();
char[]nums=新字符[numbers.length()];
System.out.println(“”);
对于(int i=0;i
char[]nums=new char[length1]处
length1的值是多少
nums
是零长度数组,因为
length1=0
num
是零长度。但是您尝试在
循环的第一次迭代中设置
num
(即
num[0]
)的第一个元素。