2015-03-18 56 views
1

我目前在我的数组中遇到了一个越​​界问题,而且我真的不确定我到底在哪里出错了。我真的很想再看看这件事情​​,因为我会失去理智。数组索引超出范围例外问题

我诚挚地感谢任何帮助。 不是:谢谢。

package com.jpmorgan.spring.csv; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class CSVRead { 
    static void read() throws IOException { 
     String csvFileToRead = "profit.csv"; 
     BufferedReader br = null; 
     String line = ""; 
     String splitBy = ","; 

     try { 
      br = new BufferedReader(new FileReader(csvFileToRead)); 

      while ((line = br.readLine()) != null) { 
       String[] array = line.split(splitBy); 
       System.out.println("Equity & Bonds: [Instrument Type= " + array[0] + " , Name=" + array[1] + " , Quantity=" + array[2] 
           + " , Buy=" + array[3] + " , Sell=" + array[4] + /*" , Coupon=" + array[5] +*/ "]"); 
      } 

     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     System.out.println("Done reading CSV file"); 
    } 
} 

这是完整的CSV文件。 我试过使用调试,但它没有太大的帮助。

instrument_type,name,quantity,buy_price,sell_price,coupon 
Equity,AAA,123,1.01,1.10 
Equity,BBBB,3,1.05,1.01 
Bond,CCC,3,,,0.13 
Equity,AAA,12,1.11,1.13 
Bond,DD,3,,,1.24 

主要参考。

/** 
* Main class is menu driven, receives CSV input. 
* This program reads, calculates and writes CSV files. 
* @author David McNeill 
* @version 1.0 - 17/03/1015 
*/ 

package com.jpmorgan.spring.csv; 
import java.util.Scanner; 
public class CSVMain{ 

    public static void main(String[] arg) throws Exception { 
     @SuppressWarnings("resource") 
     Scanner in = new Scanner(System.in); 
     int userChoice; 
     boolean quit = false; 
     do { 
     System.out.println("Please choose an option using 1 - 4");      //print text to screen 
     System.out.println("------------------------------------");      //print text to screen 
     System.out.println("1: Read 'input' CSV file");         //print text to screen 
     System.out.println("2: Calculate 'input' CSV file");       //print text to screen 
     System.out.println("3: Write calculation result to CSV file");     //print text to screen 
     System.out.println("4: Exit program");           //print text to screen 
      userChoice = in.nextInt();             //'in' equals integer 
      if (userChoice == 4)              //when '3' is input then... 
        quit = true;               //the program will now quit 
      else if (userChoice == 1)             //when '1' is input then... 
        CSVRead.read(); 
      else if (userChoice == 2); 
       //####################calculations go here#########################    
       //####################calculations go here######################### 
       //####################calculations go here######################### 
      else if (userChoice == 3) 
        CSVWrite.write(); 
     } while (!quit); 
    } 
} 
+0

u能更新与CSV的前几排的问题做CSVRead.main();? – 2015-03-18 21:15:05

+0

你的代码假设'line.split(splitBy)'将返回一个至少有5个条目的数组(6个带有注释掉的位)。显然它不是。所以你必须看看这条线并找出原因。调试器是最好的方法,单步执行代码。 – 2015-03-18 21:15:18

+0

是的,从'split'方法调试它。此外,如果文件有一些不同的标题,则会导致问题。这就是为什么我问前2-3排。 – 2015-03-18 21:16:46

回答

0

分割线后,你应该确保你得到正确的列数。

我无法告诉你如何解决潜在的不良数据,但我可以帮助您识别它。只是这种替换内环:

int lineNum = 0; 
while ((line = br.readLine()) != null) { 
    String[] array = line.split(splitBy); 
    if (array.length < 5) 
     throw new Exception("There's a problem with " + csvFileToRead + " on line " + lineNum + ":\n" + line);   
    System.out.println("Equity & Bonds: [Instrument Type= " + array[0] + " , Name=" + array[1] + " , Quantity=" + array[2] 
          + " , Buy=" + array[3] + " , Sell=" + array[4] + /*" , Coupon=" + array[5] +*/ "]"); 
    lineNum++; 
} 
+0

我该怎么做到这一点? – 2015-03-18 21:42:57

+0

@David我添加了一个示例来帮助您调试问题。 – axblount 2015-03-18 21:55:48

+0

谢谢。您的异常现在似乎显示csv的第0行有问题。 – 2015-03-18 22:02:39

0

您CSVRead没有主要方法..电话更改为CSVRead.read();

import java.util.Scanner; 
public class CSVMain{ 

    public static void main(String[] arg) throws Exception { 
     @SuppressWarnings("resource") 
     Scanner in = new Scanner(System.in); 
     int userChoice; 
     boolean quit = false; 
     do { 
     System.out.println("Please choose an option using 1 - 4");      //print text to screen 
     System.out.println("------------------------------------");      //print text to screen 
     System.out.println("1: Read 'input' CSV file");         //print text to screen 
     System.out.println("2: Calculate 'input' CSV file");       //print text to screen 
     System.out.println("3: Write calculation result to CSV file");     //print text to screen 
     System.out.println("4: Exit program");           //print text to screen 
      userChoice = in.nextInt();             //'in' equals integer 
      if (userChoice == 4)              //when '3' is input then... 
        quit = true;               //the program will now quit 
      else if (userChoice == 1)             //when '1' is input then... 
        CSVRead.read(); 
      else if (userChoice == 2); 
       //####################calculations go here#########################    
       //####################calculations go here######################### 
       //####################calculations go here######################### 
     } while (!quit); 
    } 
} 

CSVRead.java

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class CSVRead { 
    static void read() throws IOException { 
     String csvFileToRead = "profit.csv"; 
     BufferedReader br = null; 
     String line = ""; 
     String splitBy = ","; 

     try { 
      br = new BufferedReader(new FileReader(csvFileToRead)); 

      while ((line = br.readLine()) != null) { 
       String[] array = line.split(splitBy); 
       System.out.println("Equity & Bonds: [Instrument Type= " + array[0] + " , Name=" + array[1] + " , Quantity=" + array[2] 
           + " , Buy=" + array[3] + " , Sell=" + array[4] + /*" , Coupon=" + array[5] +*/ "]"); 
      } 

     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     System.out.println("Done reading CSV file"); 
    } 
} 

输出

Please choose an option using 1 - 4 
------------------------------------ 
1: Read 'input' CSV file 
2: Calculate 'input' CSV file 
3: Write calculation result to CSV file 
4: Exit program 
1 
Equity & Bonds: [Instrument Type= instrument_type , Name=name , Quantity=quantity , Buy=buy_price , Sell=sell_price] 
Equity & Bonds: [Instrument Type= Equity , Name=AAA , Quantity=123 , Buy=1.01 , Sell=1.10] 
Equity & Bonds: [Instrument Type= Equity , Name=BBBB , Quantity=3 , Buy=1.05 , Sell=1.01] 
Equity & Bonds: [Instrument Type= Bond , Name=CCC , Quantity=3 , Buy= , Sell=] 
Equity & Bonds: [Instrument Type= Equity , Name=AAA , Quantity=12 , Buy=1.11 , Sell=1.13] 
Equity & Bonds: [Instrument Type= Bond , Name=DD , Quantity=3 , Buy= , Sell=] 
Done reading CSV file 

ALTERNATIVEL Y,在CSVRead

class public class CSVRead { 
    static void main() throws IOException { 

重命名read方法main然后调用它就像你在CSVMain

+0

刚试过用我的实际主类运行这个,我仍然得到同样的例外,我超出了界限。这非常令人沮丧。我的主要课程是通过设置其他菜单来调用此方法。 – 2015-03-18 21:47:34

+0

你的主要课程是什么?你显然是在做别的错误,因为我刚刚运行它,它按我告诉过的那样工作 – adrCoder 2015-03-18 21:49:06

+0

编辑第一篇文章与整个主 – 2015-03-18 21:50:10