0

我一直在开发一个程序来从文件中收集数据并对其进行一些处理(如代码和伪代码中所示),但是我很难将字符串的每个元素放入正确的数组中。文件中的一行如下所示:

1980 Aug    945 100 Allen

我想使用 substring 方法,因为这在我看来是最简单的。有更好的方法吗?到目前为止,这是我的代码。问题究竟出在哪里,我应该如何解决?谢谢!:)

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.lang.String; 
public class Hurricanes2
{

public static void main(String [] args) throws IOException 
{
int counter = 0;
String [] token = new String[1000];

String [] tokenElements = new String[128];
String [] hurricaneYear = new String[64];
String [] hurricaneName = new String[64];
String [] hurricaneMonth = new String[64];
int []  hurricaneCategory = new int[64];
double [] hurricanePressure = new double[64];
double tempKnots;
double knotsToMph; 
double [] hurricaneWindSpeed = new double[64];
double categoryAverage;
double pressureAverage;
double speedAverage; 
String headerData = "                          Hurricanes 1980 - 2006\n\n Year         Hurricane       Category        Pressure(MB)        Wind Speed     (MPH)\n========================================================================";
Scanner in = new Scanner(System.in);
Scanner inFile = new Scanner(new File("hurcData2.txt"));
System.out.println(headerData);

/**---Use for-each (line:token) 
 * Parse for year - > year array
 * parse for name - > name array
 * parse for knots - > tempKnots
 * knotsToMph = tempKnots  * 1.15078
 * hurricaneWindSpeed[counter] = knotsToMph
 *  enter if-else to calculate category (hurricaneCategory [] = 1,2,3,4, or 5):
 *      74-95 cat1 
 *      96-110 cat2
 *      111 - 129 cat3
 *      130-156 cat4
 *      157 or higher cat 5
 * 
 * 
 */
 while(inFile.hasNextLine()){

        token[counter] = inFile.nextLine();
        String tempToken = token[counter]; 
        hurricaneYear[counter] = tempToken.substring(0, 3);
        hurricaneMonth[counter] = tempToken.substring(6, 8);
        hurricanePressure[counter] = Double.parseDouble(tempToken.substring(10, 12));
        hurricaneWindSpeed[counter] = Double.parseDouble(tempToken.substring(14, 16));
        hurricaneName[counter] = tempToken.substring(17);


        counter++;


   }
System.out.print("Lines: " + counter);
}
}
4

3 回答 3

0

题名列出了index out of boundsException,但没有说明是StringIndexOutOfBoundsException还是ArrayIndexOutOfBoundsException。

您将这些行读入一个定义为包含 1000 个元素的数组,然后将解析后的字符串片段放入仅定义为包含 64 个元素的数组中。

可能是您的解析逻辑正常,但数组已满。

于 2013-11-04T18:20:46.627 回答
0

你可能会做这样的事情:

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







public class test {

    public void converter(String[] stringArray) {


        int counter = 0;
        String [] token = new String[1000];
        String [] tokenElements = new String[128];
        String [] hurricaneYear = new String[64];
        String [] hurricaneName = new String[64];
        String [] hurricaneMonth = new String[64];
        int []  hurricaneCategory = new int[64];
        double [] hurricanePressure = new double[64];
        double tempKnots;
        double knotsToMph; 
        double [] hurricaneWindSpeed = new double[64];
        double categoryAverage;
        double pressureAverage;
        double speedAverage; 
        String headerData = "                          Hurricanes 1980 - 2006\n\n Year         Hurricane       Category        Pressure(MB)        Wind Speed     (MPH)\n========================================================================";
        Scanner in = new Scanner(System.in);
        Scanner inFile = null;
        try {
            inFile = new Scanner(new File("hurcData2.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        while(inFile.hasNextLine()){

          String line = inFile.nextLine();
          String[] arrayOfString = line.split(" "); 




           }
        System.out.print("Lines: " + counter);
        }




        }

不兼容类型的问题是因为您必须将 Scanner 对象转换为字符串。我在 while 循环中这样做了,所以现在当扫描仪有下一行时,它会将该行转换为字符串并将其拆分为空格。您将拥有一个包含所有值的数组。现在您可以根据数组中的位置执行您的逻辑。

于 2013-11-04T17:38:13.770 回答
0

您可以使用正则表达式将字符串拆分为数组:

    String s = "1980 Aug    945 100 Allen";
    Pattern p = Pattern.compile("\\s+");
    System.out.println(Arrays.toString(p.split(s)));

或使用 String.split 方法:

    System.out.println(Arrays.toString(s.split("\\s+")));

输出:

[1980, Aug, 945, 100, Allen]
[1980, Aug, 945, 100, Allen]
于 2013-11-04T17:38:50.490 回答