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