Java 从数组的索引中检索字符串,但它';它不工作了

Java 从数组的索引中检索字符串,但它';它不工作了,java,arrays,for-loop,Java,Arrays,For Loop,所以,我的代码应该在输入文件中查看它包含的字符串,在有空格的地方拆分它们,然后分别输出字符串。我试着用一个数组把我分割的字符串分配给变量,这样我就可以在我想打印出来的时候访问它们,但我一直在 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at Coma.main(Coma.java:26) 有人能帮我吗?请原谅我格式化这个问题,因为这是我第一次使用StackOverflow 这是我的密码 imp

所以,我的代码应该在输入文件中查看它包含的字符串,在有空格的地方拆分它们,然后分别输出字符串。我试着用一个数组把我分割的字符串分配给变量,这样我就可以在我想打印出来的时候访问它们,但我一直在

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at Coma.main(Coma.java:26)
有人能帮我吗?请原谅我格式化这个问题,因为这是我第一次使用StackOverflow

这是我的密码

import java.io.File;
import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.lang.ArrayIndexOutOfBoundsException;

public class Coma {

public static void main(String[] args)throws IOException {
    // TODO Auto-generated method stub
    String SENTENCE;
    int NUM_LINES;
    Scanner sc= new Scanner(new File("coma.in"));
    NUM_LINES=sc.nextInt();

    for(int i=0;i<NUM_LINES;i++){
        SENTENCE=sc.nextLine();
        String [] temp;
        String delimiter=" ";
        temp=SENTENCE.split(delimiter);
        String year= temp[0];
        String word=temp[1];

        System.out.println("Nurse: Sir you've been in a coma since " + year     + "\nMe: How's my favorite " + word + " doing?");
    }
}

} 

问题很可能是您的coma.in文件格式。 但是,假设文件格式正确,如下所示:

data.txt

20队

10只狗

您可以将代码简化为:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("data.txt"));
        // default delimiter is whitespace (Character.isWhitespace)
        while (sc.hasNext()) { // true if another token to read
             System.out.println("Nurse: Sir you've been in a coma since "
                     + sc.next() + "\nMe: How's my favorite "
                     + sc.next() + " doing?");
        }
    }

}

问题很可能是您的coma.in文件格式。 但是,假设文件格式正确,如下所示:

data.txt

20队

10只狗

您可以将代码简化为:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("data.txt"));
        // default delimiter is whitespace (Character.isWhitespace)
        while (sc.hasNext()) { // true if another token to read
             System.out.println("Nurse: Sir you've been in a coma since "
                     + sc.next() + "\nMe: How's my favorite "
                     + sc.next() + " doing?");
        }
    }

}

假设您的文件格式如下:

2
1981 x
1982 y
然后

将只读取
2
并停在那里,而不会使用换行符!因此,为了读取下一行(
1981 x
),您必须添加另一个
sc.nextLine()
,以实际使用2后面的(空)字符串,才能到达下一行。然后,您将拆分空字符串,这将导致
ArrayIndexOutOfBoundsException
,因为生成的数组长度仅为
1

//...
NUM_LINES=sc.nextInt();
sc.nextLine();  // add this line; 

for(int i=0;i<NUM_LINES;i++){
    SENTENCE=sc.nextLine();
    //...

假设您的文件格式如下:

2
1981 x
1982 y
然后

将只读取
2
并停在那里,而不会使用换行符!因此,为了读取下一行(
1981 x
),您必须添加另一个
sc.nextLine()
,以实际使用2后面的(空)字符串,才能到达下一行。然后,您将拆分空字符串,这将导致
ArrayIndexOutOfBoundsException
,因为生成的数组长度仅为
1

//...
NUM_LINES=sc.nextInt();
sc.nextLine();  // add this line; 

for(int i=0;i<NUM_LINES;i++){
    SENTENCE=sc.nextLine();
    //...
您可以替换:

NUM_LINES=sc.nextInt();
作者:

它会很好地工作。

您可以替换:

NUM_LINES=sc.nextInt();
作者:


它可以正常工作。

您确定
temp
包含多个元素吗?您应该首先检查
split
调用是否实际拆分了某些内容(并且不返回大小为1的数组)。问题在于您的输入数据。请提供文件中的一些行如果整数的行数在其自身的行中,您可能需要添加
sc.nextLine(),如
sc.nextInt()不会将
扫描仪
移动到下一行。因此,第一个
sc.nextLine()只读取
NUM_行
行的剩余部分(空)。是否确定
temp
包含多个元素?您应该首先检查
split
调用是否实际拆分了某些内容(并且不返回大小为1的数组)。问题在于您的输入数据。请提供文件中的一些行如果整数的行数在其自身的行中,您可能需要添加
sc.nextLine(),如
sc.nextInt()不会将
扫描仪
移动到下一行。因此,第一个
sc.nextLine()只读取
NUM\u行的剩余部分(空)。