2015-09-27 5 views
0

Мне удалось разобрать первые значения из текстового файла, из которых 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 

проблема в цикле, пока не работает, как ожидалось. сначала я использовать разделитель как Whitespace и я получить 18 метрических значений

Во-вторых, я использовал delimiterTwo как «пробельных-тильда-Whitespace» «~», чтобы получить «name2» и «» значение куб.см

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 
     } 

, но я получаю сообщение об ошибке

ошибке исключение в потоке «главный» 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" ; 
     } 

    } 
+0

oh столько кода :) – Satya

+0

все внутри цикла while –

+0

Не знаете, почему вы все это делаете. Вам не нужны строки, где все числа (то есть просто строки, которые не начинаются с пробела)? – RealSkeptic

ответ

0

Ваш метод createMetric нужен String [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 


    } 

и вы отправляете String [2]:

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

Это является причиной, вы получаете в ArrayOutOfBoundsException

Возможно, вы должны заменить nameValue другим String[] в этой строке:

Metrics valueOfMetric = createMetric(theArrayYouReallyWantedToSend); 
Смежные вопросы