1

So i am working on something to read the data from the file below and store the data into arrays of objects, so that i can search through those arrays.

YEAR,FIRST,SECOND,THIRD,FOURTH,GRAND-FINAL-PLAYED,WINNING-SCORE,LOSING-SCORE,CROWD
1908,Souths,Easts,Souths,Cumberland,Y,14,12,4000 
1909,Souths,Balmain,Souths,Wests,N 
1910,Newtown,Souths,Newtown,Wests,Y,4,4,14000 
1911,Easts,Glebe,Glebe,Balmain,Y,11,8,20000
1912,Easts,Glebe,Easts,Wests,N

As you can see the grand final is not played every year so some arrays for the score and crowd will be left blank. Im confused like nothing else and just cant seem to do this. This is my code so far below and as you can see its nothing too flash so any help is apprciated.

String file ="data.txt";        

//reading   
try{
    InputStream ips=new FileInputStream(file); 
    InputStreamReader ipsr=new InputStreamReader(ips);
    BufferedReader br=new BufferedReader(ipsr);


    String line;
    while ((line=br.readLine())!=null){



        int commaIdx = 0;

        commaIdx = line.indexOf(",", commaIdx);

        int arrayIdx = 0;
        int beginIdx = 0;
        int lineNum = 1;

        String[] array1 = null;
        array1[lineNum] =" ";

        String[] array2 = null;
        array2[lineNum] =" ";

        String[] array3 = null;
        array3[lineNum] =" ";

        String[] array4 = null;
        array4[lineNum] =" ";

        String[] array5 = null;
        array5[lineNum] =" ";

        String[] array6 = null;
        array6[lineNum] =" ";

        String[] array7 = null;
        array7[lineNum] =" ";

        String[] array8 = null;
        array8[lineNum] =" ";

        String[] array9 = null;
        array9[lineNum] =" ";


            while (commaIdx > 1)
            {
                String theValue = line.substring(beginIdx, commaIdx - 1);
                switch (arrayIdx)
                {
                case 1:
                    array1[lineNum] = theValue;
                    break;
                case 2:
                    array2[lineNum] = theValue;
                    break;
                case 3:
                    array3[lineNum] = theValue;
                    break;
                case 4:
                    array4[lineNum] = theValue;
                    break;
                case 5:
                    array5[lineNum] = theValue;
                    break;
                case 6:
                    array6[lineNum] = theValue;
                    break;
                case 7:
                    array7[lineNum] = theValue;
                    break;
                case 8:
                    array8[lineNum] = theValue;
                    break;
                case 9:
                    array9[lineNum] = theValue;
                    break;
                }


        arrayIdx++;
        beginIdx = commaIdx + 1;
        commaIdx = line.indexOf(",", commaIdx+1);

    }



    lineNum++;
    }
    br.close(); 

}       
catch (Exception e){
    System.out.println(e.toString());
} 
4

3 回答 3

0
String[] array1 = null;

这就是您的问题所在,您必须使用您希望它们保存的数据数量来实例化数组。
如果您不知道该号码,您可以使用ArrayListLinkedList

我认为您还会发现该String.split(regex)方法很有用,因为它允许您通过使用给定的分隔符拆分字符串来创建字符串数组,如下所示:

while ((line=br.readLine())!=null){
    String[] array = line.split(",");

最后,如果你的文件超过 9 行,你会遇到很多问题,因为你只使用了 9 个数组。你应该使用一个结构来保存你的数组,而不用命名它们:

List<String[]> listOfArray = new LinkedList();
while((line=br.readLine())!=null) {
    listOfArray.add(line.split(","));
}

这几乎可以满足您的需求。要使用数组之一:

String[] array = listOfArray.get(IndexOfTheArray);
于 2013-05-28T09:53:09.730 回答
0

使用String.split()方法,使用逗号作为分隔符。应该毫不费力地工作。然后只需检查返回数组的大小,就可以知道是否进行了总决赛。

你也可以使用StringTokenizer类。

于 2013-05-28T09:49:28.897 回答
0

数组必须是intanciated!您需要事先知道它的大小:

String[] array1 = new String[size];
于 2013-05-28T09:46:25.160 回答