继续获取java.lang.ArrayIndexOutOfBoundsException并且无法确定原因

时间:2013-11-11 03:00:15

标签: java arrays exception indexing

处理将从文件读取输入的程序(使用独立声明作为测试)并将整数,字符,字符串等写入另一个新文件。但我遇到的问题是我的数组索引超出界限。我现在已经看过我的循环了十几次,但我无法弄清楚是什么原因导致了这个问题。在此先感谢任何帮助=]

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

public class P5 {

    static String arrayString[] = new String[100];
    static int numberLines = 0;
    static int numberWords = 0;
    static int numberChars = 0;
    static int numberUpper = 0;
    static int numberLower = 0;
    static int numberDigits = 0;
    static int numSpaces = 0;
    static int numberTabs = 0;
    static int numberSpecial = 0;


    private static void readFile(String inputFile) {    
        try{

            Scanner fileScan = new Scanner(new File("Declaration.txt"));

            while(fileScan.hasNext()){

                arrayString[numberWords] = fileScan.next();
                numberWords++;
            }

            while(fileScan.hasNextLine()){
                numberLines++;
            }

            fileScan.close();
        }

        catch(IOException e){
        }
    }   

    private static void gatherStatistics(String sArray[]) {
        for (int i = 0; i < sArray.length; i++){

            String line = sArray[i];
            numberChars += line.length();

            for (int j = 0; j < line.length(); j++){

                char c = line.charAt(j);    

                if(Character.isUpperCase(c))
                    numberUpper++;

                else if(Character.isLowerCase(c))
                    numberLower++;

                else if(Character.isDigit(c))
                    numberDigits++;

                else if(Character.isSpaceChar(c))
                    numSpaces++;

                else if(c == '\t')
                    numberTabs++;

                else
                    numberSpecial++;
            }
        }
    }

    private static void writeFile(String outputFile) {

        try{

            PrintWriter fileOut = new PrintWriter(new File("Statistics.txt"));
            fileOut.println("Number of Lines: " + numberLines);
            fileOut.println("Number of Words: " + numberWords);
            fileOut.println("Number of Lines: " + numberChars);
            fileOut.println("Number of Lines: " + numberUpper);
            fileOut.println("Number of Lines: " + numberLower);
            fileOut.println("Number of Lines: " + numberDigits);
            fileOut.println("Number of Lines: " + numSpaces);
            fileOut.println("Number of Lines: " + numberTabs);
            fileOut.println("Number of Lines: " + numberSpecial);

            fileOut.close();
            System.exit(1);

        } catch (FileNotFoundException e){}
    }

    public static void main(String[] args) {

        readFile(args[0]);
        gatherStatistics(arrayString);
        writeFile(args[1]);
        System.exit(1);

    }
}

1 个答案:

答案 0 :(得分:3)

您已阅读超过100行的文件。

使用ArrayList

而不是使用数组

更改此行:

static String arrayString[] = new String[100];

为:

static ArrayList<String> arrayString = new ArrayList<String>();

更改此行:

private static void gatherStatistics(String sArray[]) {

为:

private static void gatherStatistics(ArrayList<String> sArray) {

更改此行:

arrayString[numberWords] = fileScan.next();

arrayString.add(fileScan.next());