Substring - StringIndexOutOfBounds异常java

时间:2013-11-04 17:30:19

标签: java exception substring

我一直在研究一个从文件中收集数据的程序并对它做一些事情(如代码和伪代码中所示)但是我很难将字符串的每个元素都放到正确的数组中。文件中的一行如下所示:

1980 Aug    945 100 Allen

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

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

3 个答案:

答案 0 :(得分: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对象转换为String。我在while循环中做到了这一点,所以现在当扫描器有下一行时,它会将该行转换为字符串并将其拆分为空格。您将拥有一个包含所有值的数组。现在,您可以根据数组中的位置执行逻辑。

答案 1 :(得分: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]

答案 2 :(得分:0)

问题的标题列出了索引超出范围的异常,但它没有说明它是否是StringIndexOutOfBoundsException或ArrayIndexOutOfBoundsException。

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

可能是你的解析逻辑没问题,但数组已经过度填充。