如何解析嵌套文本文件?

时间:2015-09-27 19:55:39

标签: java csv text-parsing string-parsing

我设法解析文本文件中的第一个值是18个度量值,并将其转换为所需的格式。

文本文件

superclass1 7 1 0 6 30 11 2 6 5 0.6667 218 1.0000 0 0.0000 0.2000 0 0 29.8571

     ~ public static boolean isJdkIncluded(): 1

     ~ static void processClass(gr.spinellis.ckjm.ClassMetricsContainer arg0, String arg1): 3

     ~ public static void runMetrics(String[] arg0, gr.spinellis.ckjm.CkjmOutputHandler arg1): 2

     ~ public static boolean includeAll(): 2

     ~ static void <clinit>(): 1

     ~ public static void main(String[] arg0): 7

     ~ public void <init>(): 1
       whitespace1 
       whitespace2 
       whitespace3
superclass2  2 1 0 3 8 0 1 2 2 0.0000 24 1.0000 0 0.0000 0.6250 0 0 10.5000

     ~ public void handleClass(String arg0, gr.spinellis.ckjm.ClassMetrics arg1): 1

     ~ public void <init>(java.io.PrintStream arg0): 1

问题是在while循环中没有按预期工作。 首先我使用分隔符作为空格,我得到18个度量值

第二个是我用delimiterTwo作为“Whitespace-tilde-Whitespace”“〜”得到“name2”和“cc”值

  while (line != null) {

    String[] attributes = line.split(" ");  // the file, using a Whitespace as the delimiter
                    Metrics valueOfMetric = createMetric(attributes);
                    metricsss.add(valueOfMetric);      // adding metric  into ArrayList
                    line = br.readLine();


            // parse delimiter as "Whitespace-tilde-Whitespace"     " ~ "

            String[] delimiterTwo = line.split(" ~ ");
            if (delimiterTwo.length == 2) {
                String[] nameValue = delimiterTwo[1].split(": ");
                if (nameValue.length == 2) {
                    Metrics valueOfMetric = createMetric(nameValue);
                    metricsss.add(valueOfMetric);// adding book into ArrayList
                             }
            }              
            line = br.readLine();// read next line before looping
        }

但我收到此错误消息

错误线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:19

这是我写的代码

    package javaapplication33;
  package javaapplication39;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;

    /*  to read CSV file in Java. In this program we will read * list of metrics  stored in CSV file as comma separated values. */
    public class readallvalues {

        public static void main(String... args) {
            List<Metrics> metric = readMetricFromCSV("C:\\Users\\hp\\Desktop\\101.txt");

            // let's print all the metric read from CSV file
            for (Metrics m : metric) {
                System.out.println(m);
            }
        }

        private static List<Metrics> readMetricFromCSV(String fileName) {
            List<Metrics> metricsss = new ArrayList<>();
            Path pathToFile = Paths.get(fileName);

            // create an instance of BufferedReader
            // using try with resource, Java 7 feature to close resources
            try (BufferedReader br = Files.newBufferedReader(pathToFile,                StandardCharsets.US_ASCII)) {

                br.readLine();
                String line1=null;
                // read the first line from the text file
                String line = br.readLine();

                // loop until all lines are read
                while (line != null) {

                    // use string.split to load a string array with the values from
                    // each line of
                    // the file, using a comma as the delimiter
                    //delimiter by Whitespace  gr.spinellis.ckjm.ClassVisitor 13 2 0 14 74 34 2 14 9 0.6042 431 0.8750 1 0.7273 0.2404 0 0 31.5385  
                    String[] attributes = line.split(" ");//read first row which has 18 metrics values 

                    Metrics valueOfMetric = createMetric(attributes);

                   // internal loop   ***delimiter by Whitespace-tilde-Whitespace
                    // ~ public void visitMethod(org.apache.bcel.classfile.Method arg0): 4

                     String[] delimiterTwo = line.split(" ~ ");//read sub row which has 2 vlues name + value 
                     if (delimiterTwo.length == 2) {
                          String[] name2 = delimiterTwo[1].split(": ");
                             if (name2.length == 2) {
                                 valueOfMetric = createMetric(name2);
                                metricsss.add(valueOfMetric);// adding book into ArrayList
                                         }
                               }              
                    // adding metric  into ArrayList
                    metricsss.add(valueOfMetric);

                    // read next line before looping
                    line = br.readLine();
                }


            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

            return metricsss;
        }

        private static Metrics createMetric(String[] metadata) {
            //  classname, WMC, DIT, NOC, CBO, RFC,LCOM, Ca, Ce, NPM,LCOM3,LOC, DAM, MOA, MFA, CAM,IC, CBM and AMC .WMC + cc

            String name = metadata[0];
            int WMC = Integer.parseInt(metadata[1]);
            int DIT = Integer.parseInt(metadata[2]);
            int NOC = Integer.parseInt(metadata[3]);
            int CBO = Integer.parseInt(metadata[4]);
            int RFC = Integer.parseInt(metadata[5]);
            int LCOM= Integer.parseInt(metadata[6]);
            int Ca  = Integer.parseInt(metadata[7]);
            int Ce  = Integer.parseInt(metadata[8]);
            int NPM = Integer.parseInt(metadata[9]);
            Double LCOM3= Double.parseDouble(metadata[10]);
            int LOC = Integer.parseInt(metadata[11]);
            Double DAM = Double.parseDouble(metadata[12]);
            int MOA = Integer.parseInt(metadata[13]);
            Double MFA = Double.parseDouble(metadata[14]);
            Double CAM = Double.parseDouble(metadata[15]);
            int IC  = Integer.parseInt(metadata[16]);
            int CBM = Integer.parseInt(metadata[17]);
            Double AMC = Double.parseDouble(metadata[18]);
            String name2=  (metadata[19]);
            int cc  = Integer.parseInt(metadata[20]);
            // create and return metric  of this metadata
           //WMC, DIT, NOC, CBO, RFC,LCOM, Ca, Ce, NPM,LCOM3,LOC, DAM, MOA, MFA, CAM,IC, CBM and AMC ,name2,cc
            return new Metrics(name,WMC,DIT,NOC,CBO,RFC,LCOM,Ca,Ce,NPM,LCOM3,LOC,DAM,MOA,MFA,CAM,IC,CBM ,AMC,name2,cc);//,cc


        }

    }

    class Metrics {

    private String name;
    private int WMC;
    private int DIT;
    private int NOC;
    private int CBO;
    private int RFC;
    private int LCOM;
    private int Ca;
    private int Ce;
    private int NPM;
    private Double LCOM3;
    private int LOC;
    private Double DAM;
    private int MOA;
    private Double MFA;
    private Double CAM;
    private int IC;
    private int CBM ;
    private Double AMC;
    private String name2 ; 
    private int cc  ;  


    //String name,int WMC,int DIT,int NOC,int CBO,int RFC,int LCOM, int Ca, int Ce, int NPM,int LCOM3,
    //int LOC,int DAM,int MOA,int MFA,int CAM, int IC,int CBM ,int AMC ,String name2 ,int cc   

        public Metrics( String name,int WMC,int DIT,int NOC,int CBO,int RFC,int LCOM, int Ca, int Ce, int NPM,Double LCOM3,
                                    int LOC,Double DAM,int MOA,Double MFA,Double CAM, int IC,int CBM ,Double AMC,String name2,  int cc) {
            this.name = name;
            this. WMC  =WMC ;
            this. DIT  =DIT ;
            this. NOC = NOC;
            this. CBO =CBO ;
            this. RFC = RFC;
            this.LCOM = LCOM;
            this. Ca = Ca;
            this.Ce =Ce ;
            this. NPM = NPM;
            this.LCOM3 = LCOM3;
            this. LOC = LOC;
            this. DAM = DAM;
            this.MOA  =MOA ;
            this. MFA =MFA ;
            this. CAM =CAM ;
            this. IC = IC ;
            this. CBM  =CBM ;
            this. AMC = AMC ;
            this. name2 = name2; 
           this. cc = cc;  

        }

        public String getName() {        return name;    }
        public void setName(String name) {        this.name = name;    }

    public int getWMC()          {         return WMC     ;     }
    public void setWMC(int WMC)  {        this.WMC = WMC  ;     }

        //WMC, DIT, NOC, CBO, RFC,LCOM, Ca, Ce, NPM,LCOM3,LOC, DAM, MOA, MFA, CAM,IC, CBM and AMC ,name2 ,cc
    public int getDIT()          {         return DIT     ;     }
    public void setDIT(int DIT)  {        this.DIT = DIT  ;     }


    public int getNOCC()          {         return NOC     ;     }
    public void setNOC(int NOC)  {        this.NOC = NOC  ;     }


    public int getCBO()          {         return CBO     ;     }
    public void setCBO(int CBO)  {        this.CBO = CBO  ;     }

    public int getRFC()          {         return RFC     ;     }
    public void setRFC(int RFC)  {        this.RFC = RFC  ;     }

    public int getLCOM()          {         return LCOM     ;     }
    public void setLCOM(int LCOM)  {        this.LCOM = LCOM  ;     }

    public int getCa()          {         return Ca     ;     }
    public void setCa(int Ca)  {        this.Ca = Ca  ;     }

    public int getCe()          {         return Ce     ;     }
    public void setCe(int Ce)  {        this.Ce = Ce  ;     }

    public int getNPM()          {         return NPM     ;     }
    public void setNPM(int NPM)  {        this.NPM = NPM  ;     }

    public Double getLCOM3()          {         return LCOM3     ;     }
    public void seLCOM3(Double LCOM3)  {        this.LCOM3 = LCOM3  ;     }

    public int getLOC()          {         return LOC     ;     }
    public void setLOC(int LOC)  {        this.LOC = LOC  ;     }

    public Double getDAM()          {         return DAM     ;     }
    public void setDAM(Double DAM)  {        this.DAM = DAM  ;     }

    public int getMOA()          {         return MOA     ;     }
    public void setMOA(int MOA)  {        this.MOA = MOA  ;     }

    public Double getMFA()          {         return MFA     ;     }
    public void setMFA(Double MFA)  {        this.MFA = MFA  ;     }

    public Double getCAM()          {         return CAM     ;     }
    public void setCAM(Double CAM)  {        this.CAM = CAM  ;     }

    public int getIC()          {         return IC     ;     }
    public void setIC(int IC)  {        this.IC = IC  ;     }

    public int getCBM()          {         return CBM     ;     }
    public void setCBM(int CBM)  {        this.CBM = CBM  ;     }


    public Double getAMC()          {         return AMC     ;     }
    public void setAMC(Double AMC)  {        this.AMC = AMC  ;     }

   public String getname2()          {         return name2     ;     }
    public void setname2(String name2)  {        this.name2 = name2  ;     }

    public int getcc()          {         return cc     ;     }
    public void setcc(int cc)  {        this.cc = cc  ;     }


        @Override
        public String toString() {
            return "name=  " + name +" WMC= " + WMC + " DIT= " + DIT + " NOC " + NOC   + " CBO " + CBO 
                    + " RFC " + RFC + " LCOM " + LCOM + " Ca " + Ca + " Ce " + Ce + " NPM " + NPM 
                    + " LCOM3 " + LCOM3 + " LOC " + LOC + " DAM " + DAM + " MOA " + MOA + " MFA " + MFA 
                    + " CAM " + CAM + " IC " + IC + " CBM " + CBM + " AMC " + AMC + " Name2 " + name2+" CC "+cc+"\n\n" ;
        }

    }

1 个答案:

答案 0 :(得分:0)

您的createMetric方法需要字符串[21]

private static Metrics createMetric(String[] metadata) {
        //  classname, WMC, DIT, NOC, CBO, RFC,LCOM, Ca, Ce, NPM,LCOM3,LOC, DAM, MOA, MFA, CAM,IC, CBM and AMC .WMC + cc

        String name = metadata[0];
        int WMC = Integer.parseInt(metadata[1]);
        int DIT = Integer.parseInt(metadata[2]);
        int NOC = Integer.parseInt(metadata[3]);
        int CBO = Integer.parseInt(metadata[4]);
        int RFC = Integer.parseInt(metadata[5]);
        int LCOM= Integer.parseInt(metadata[6]);
        int Ca  = Integer.parseInt(metadata[7]);
        int Ce  = Integer.parseInt(metadata[8]);
        int NPM = Integer.parseInt(metadata[9]);
        Double LCOM3= Double.parseDouble(metadata[10]);
        int LOC = Integer.parseInt(metadata[11]);
        Double DAM = Double.parseDouble(metadata[12]);
        int MOA = Integer.parseInt(metadata[13]);
        Double MFA = Double.parseDouble(metadata[14]);
        Double CAM = Double.parseDouble(metadata[15]);
        int IC  = Integer.parseInt(metadata[16]);
        int CBM = Integer.parseInt(metadata[17]);
        Double AMC = Double.parseDouble(metadata[18]);
        String name2=  (metadata[19]);
        int cc  = Integer.parseInt(metadata[20]);
        // create and return metric  of this metadata
       //WMC, DIT, NOC, CBO, RFC,LCOM, Ca, Ce, NPM,LCOM3,LOC, DAM, MOA, MFA, CAM,IC, CBM and AMC ,name2,cc
        return new Metrics(name,WMC,DIT,NOC,CBO,RFC,LCOM,Ca,Ce,NPM,LCOM3,LOC,DAM,MOA,MFA,CAM,IC,CBM ,AMC,name2,cc);//,cc


    }

您正在发送字符串[2]:

if (nameValue.length == 2) {
    Metrics valueOfMetric = createMetric(nameValue);
    metricsss.add(valueOfMetric);// adding book into ArrayList
}

这就是您获得ArrayOutOfBoundsException

的原因

您可能应该将nameValue替换为此行中的另一个String[]

Metrics valueOfMetric = createMetric(theArrayYouReallyWantedToSend);