ArrayIndexOutOfBoundsException请帮助我

时间:2017-03-02 18:12:23

标签: java arrays exception out

我的Java代码存在问题。我尝试了一切来弄清楚错误的问题。也许你可以帮助我。

public static void Convert1() throws IOException,ArrayIndexOutOfBoundsException,NumberFormatException {
    String csv ="/Users/Max/IdeaProjects/Download/Asset1.csv";
    BufferedReader br = null;
    String line = "";
    String csvSplitBy = ";";

    File file = new File ("/Users/Max/IdeaProjects/Download/Datum1.csv");
    if (file.exists()) {

    } else {



        br = new BufferedReader(new FileReader(csv));
        while ((line = br.readLine()) != null) {


              String[] asset = line.split(csvSplitBy);


            String[] Date = {asset[0]};  //Datum
            String[] Open = {asset[1]};  //Öffnungskurs
            String[] High = {asset[2]};  //Tageshoch
            String[] Low = {asset[3]};  //Tagestief
            String[] Close = {asset[4]};  //Schlusskurs
            String[] Volume = {asset[5]};  //Handelsvolumen


            // Es werden die Dateien geschrieben die die einzelnen Teilbereiche enthalten
            BufferedWriter BW = new BufferedWriter(new FileWriter("Datum1.csv", true));
            StringBuilder SB = new StringBuilder();
            for (String element : Date) {
                SB.append(element);
                SB.append(";");
            }
            BW.write(SB.toString());
            BW.close();

            BufferedWriter BW2 = new BufferedWriter(new FileWriter("Öffnungskurs1.csv", true));
            StringBuilder SB2 = new StringBuilder();
            for (String element : Open) {
                SB2.append(element);
                SB2.append(";");
            }
            BW2.write(SB2.toString());
            BW2.close();

            BufferedWriter BW3 = new BufferedWriter(new FileWriter("Tageshoch1.csv", true));
            StringBuilder SB3 = new StringBuilder();
            for (String element : High) {
                SB3.append(element);
                SB3.append(";");
            }
            BW3.write(SB3.toString());
            BW3.close();

            BufferedWriter BW4 = new BufferedWriter(new FileWriter("Tagestief1.csv", true));
            StringBuilder SB4 = new StringBuilder();
            for (String element : Low) {
                SB4.append(element);
                SB4.append(";");
            }
            BW4.write(SB4.toString());
            BW4.close();

            BufferedWriter BW5 = new BufferedWriter(new FileWriter("Schlusskurs1.csv", true));
            StringBuilder SB5 = new StringBuilder();
            for (String element : Close) {
                SB5.append(element);
                SB5.append(";");
            }
            BW5.write(SB5.toString());
            BW5.close();

            BufferedWriter BW6 = new BufferedWriter(new FileWriter("Handelsvolumen1.csv", true));
            StringBuilder SB6 = new StringBuilder();
            for (String element : Volume) {
                SB6.append(element);
                SB6.append(";");
            }
            BW6.write(SB6.toString());
            BW6.close();
        }

    }

}

每次我想要运行时,我都会遇到以下异常。

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:1     在ConvertCSVtoArraysAsset1.Convert1(ConvertCSVtoArraysAsset1.java:30)

也许你可以帮助我。

问候

1 个答案:

答案 0 :(得分:0)

在您使用数组索引和异常中的“1”的唯一位置,错误可能就在这一行:

String[] Open = {asset[1]};

这可能是因为你的数组中没有那么多元素。

将此行放在String.split之后,看看你的数组是由什么组成的,它的长度是:

System.out.println( Arrays.toString( asset ) );