2013-10-31 50 views
-1

*抬头这在技术上是HOMEWORK,但它不会让我添加标记由于某种原因我不断收到一个java.lang.arrayindexoutofboundsexception 5的错误,但我不能找到错误

编译器似乎特别指向了specialPrintData()方法,但我真的无法弄清楚什么是错的。这是完成我的程序的最后一步,因此欢迎所有帮助!

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); 

} 
} 
+0

家庭作业不再是SO中的标记http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated –

+0

您是否还尝试过调试您的程序? –

+0

发布堆栈跟踪。 – Maximin

回答

2

countWords方法,则需要在索引i + 1访问的字符。因此,您必须像这样前检查:

if (i + 1 < str.length()) { 

当你有固定的这个问题,你会得到一个NullPointerException。查看堆栈跟踪,可以看到它发生的代码行。

要解决该问题,您应该将String[]替换为ArrayList<String>。然后你可以去掉MAX常量,因为列表就像一个数组,但是只要你有一些东西,它就会自动增长。

+0

注意OP有'str.length() - 1',但它们也有'<=',而不是'<'。这是真正的区别。 – kevinsa5

+0

但OP已经指出异常正在抛出'specialPrintData'方法。请参阅我的回答 –

+1

这里有不止一个例外。 :) –

1

问题出在您的printNicelyFormattedData方法中。在这条线

specialPrintData(list1, count); 

你逝去的String[] fd的数量,而不是String[] list计数。

而不是一行中的单词数量,您正在传递 文件中的行数。

所以不是说,使用这种

specialPrintData(list1, list1.length); 

编辑:

为@Roland提到的还有另一个可能发生,但只有在以下情况下例外。

的StringIndexOutOfBoundsException很可能在这行发生,如果(str.charAt(I)== '' & & str.charAt第(i + 1)!='“)仅当该行的最后一个字符是'',否则第一个条件将失败的最后一个字符和第二个条件将永远不会被检查

希望这可以帮助。

+0

这解决了这个问题!谢谢,非常感谢!现在我可以睡了... –

+0

欢迎..总是乐于帮助:) –