我一直收到java.lang.arrayindexoutofboundsexception 5错误,但找不到错误
*请注意,这从技术上来说是家庭作业,但出于某种原因,它不允许我添加标签 编译器似乎特别指向specialPrintData方法,但我真的不知道出了什么问题。这是完成我的程序的最后一步,欢迎所有帮助我一直收到java.lang.arrayindexoutofboundsexception 5错误,但找不到错误,java,arrays,debugging,indexoutofboundsexception,Java,Arrays,Debugging,Indexoutofboundsexception,*请注意,这从技术上来说是家庭作业,但出于某种原因,它不允许我添加标签 编译器似乎特别指向specialPrintData方法,但我真的不知道出了什么问题。这是完成我的程序的最后一步,欢迎所有帮助 import java.util.Scanner; import java.io.File; public class Lab07 { static final int MAX = 100; // global constant static public int readFile(
import java.util.Scanner;
import java.io.File;
public class Lab07
{
static final int MAX = 100; // global constant
static public int readFile( String [ ] fd, String fileName) throws Exception
{
File f = new File( fileName );
Scanner input = new Scanner( f );
int count = 0;
while( count < fd.length && input.hasNextLine( ) )
{
fd[count] = input.nextLine( );
count++;
}
if (input.hasNext( ) )
{
System.out.println("File is larger than array. Program is terminating.");
System.exit(0);
}
else
{
System.out.println("Read " + count + " int values from file.");
}
input.close( );
return count;
}
static public void printData( String [ ] fd, int count )
{
for( int i = 0; i < count; i++)
{
System.out.println( fd[i] );
}
System.out.println();
}
static public void specialPrintData( String [ ] fd, int count )
{
for( int i = 3; i < count; i++)
{
System.out.printf(" %4s ", fd[i] );
}
}
static public int countWords(String str)
{
int count = 1;
for (int i = 0; i <= str.length() - 1; i++)
{
if (str.charAt(i) == ' ' && str.charAt(i+1) != ' ')
{
count++;
}
}
return count;
}
static public void printNicelyFormattedData( String [ ] fd, int count )
{
int numKids = 0;
for( int i = 0; i < count; i++)
{
if (countWords( fd[i] ) > 2)
{
numKids = ((countWords( fd[i]) - 3));
}
String [ ] list1 = fd[i].split(" ");
System.out.println( list1[0] + " and " + list1[1] + " " + list1[2] + " have " + numKids + " children:" + "\n");
specialPrintData( list1, count);
}
System.out.println();
}
static public void main( String args [ ] ) throws Exception
{
if( args.length < 1 )
{
System.out.println("Error running program!");
System.out.println("Usage: java Lab07 L7d.txt");
System.exit( 0 );
}
String [ ] fileData = new String[MAX];
int count = 0;
count = readFile( fileData, args[0] );
System.out.println( "Array of file data contains " + count + " lines of data:");
printData( fileData, count );
System.out.println("*******************************************************");
System.out.println("Family Data for past and present presidential families:");
printNicelyFormattedData( fileData, count );
}
}
在countWords方法中,您正在访问索引i+1处的字符。因此,您必须在执行以下操作之前进行检查:
if (i + 1 < str.length()) {
修复此问题后,将得到一个NullPointerException。查看堆栈跟踪,您可以看到它发生在代码的哪一行
要解决这个问题,应该用ArrayList替换字符串[]。然后,您可以去掉MAX常量,因为列表就像一个数组,但只要向其中添加内容,它就会自动增长。问题在于printNicelyFormattedData方法。在这条线上
specialPrintData( list1, count);
您正在传递字符串[]fd的计数,而不是字符串[]列表的计数
这不是一行中的字数,而是传递该行中的行数
文件
所以用这个代替它
specialPrintData( list1, list1.length);
编辑:
正如@Roland提到的,可能会发生另一个例外,但仅在以下情况下
如果str.charAti=''&&str.charAti+1!='',则可能在此行发生StringIndexOutOfBoundsException仅当行的最后一个字符为“”时,否则第一个条件将对最后一个字符失败,而第二个条件将永远不会被检查
希望这有帮助。家庭作业不再是一个标签,所以你也试过调试你的程序了吗?发布堆栈跟踪。你能在线程主java.lang.ArrayIndexOutofBounds中放置完整的堆栈跟踪:Exception:5 at Lab07.specialPrintDataLab07.java:54 at printNicelyFormattedDataLab07.java:86 at Lab07.mainLab07.java:111编辑:抱歉,这是完整的oneNote,OP有str.length-1,但他们也有str.length-1,但OP指出,在specialPrintData方法中引发了异常。请参考我的回答这里有不止一个例外:如果str.charAti=''&&str.charAti+1!='',则可能在此行发生StringIndexOutOfBoundsException仅当行的最后一个字符为“”时,否则第一个条件将失败,而第二个条件将永远不会被检查。