Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store 2 things from text file name and values only

anything after ~ sing should be thread as one string called "Name" and anything after ":" colon should be an integer called "Value"

example of the text file

~ public void visitMethod(org.apache.bcel.classfile.Method arg0): 4

~ public void registerCoupling(String arg0): 4

~ public void end(): 5

~ public void <init>(org.apache.bcel.classfile.JavaClass arg0, gr.spinellis.ckjm.ClassMetricsContainer arg1): 1

~ public gr.spinellis.ckjm.ClassMetrics getMetrics(): 1

~ private void incRFC(String arg0, String arg1, org.apache.bcel.generic.Type[] arg2): 1

~ public void start(): 1

~ void registerMethodInvocation(String arg0, String arg1, org.apache.bcel.generic.Type[] arg2): 1

~ public void visitField(org.apache.bcel.classfile.Field arg0): 1

~ public void visitJavaClass(org.apache.bcel.classfile.JavaClass arg0): 5

~ void registerFieldAccess(String arg0, String arg1): 2

~ static String className(org.apache.bcel.generic.Type arg0): 3

~ public void registerCoupling(org.apache.bcel.generic.Type arg0): 1

~ void <init>(): 1

~ public gr.spinellis.ckjm.ClassMetrics getMetrics(String arg0): 2

~ public void printMetrics(gr.spinellis.ckjm.CkjmOutputHandler arg0): 3



this is the code i have tried

Java
package javaapplication38;
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 readingtry {
    
   
    public static void main(String... args) {
        List<Metrics> metric = readMetricFromCSV("C:\\Users\\hp\\Desktop\\alamah.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)) {

            // 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
                String[] attributes = line.split(" ~ ");

                Metrics valueOfMetric = createMetric(attributes);

                // adding book 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 classvalue  
        String name=  (metadata[0]);
          int value = Integer.parseInt(metadata[1 ]);
 
       
       //                                
        return new Metrics(name,value);
        
        
    }

}

class Metrics {
private String name;
private int value;


    public Metrics( String name,int value) {
        this.name = name;
          this.value=value;
       
    }

    public String getName() {        return name;    }
    public void setName(String name) {        this.name = name;    }
    
     public int getvalue() {        return value;    }
    public void setvalue(int value) {        this.value = value;    }
    

    @Override
    public String toString() {
        return "name=  " + name +"value =  " + value+"\n\n" ;
    }

}
Posted
Updated 27-Sep-15 6:39am
v3
Comments
Maciej Los 27-Sep-15 12:13pm    
What have you tried till now?
aloweew 27-Sep-15 12:22pm    
package javaapplication38;
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 readingtry {


public static void main(String... args) {
List<metrics> metric = readMetricFromCSV("C:\\Users\\hp\\Desktop\\alamah.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)) {

// 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
String[] attributes = line.split(" ~ ");

Metrics valueOfMetric = createMetric(attributes);

// adding book 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 classvalue
String name= (metadata[0]);
int value = Integer.parseInt(metadata[1 ]);


//
return new Metrics(name,value);


}

}

class Metrics {
private String name;
private int value;


public Metrics( String name,int value) {
this.name = name;
this.value=value;

}

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

public int getvalue() { return value; }
public void setvalue(int value) { this.value = value; }


@Override
public String toString() {
return "name= " + name +"value = " + value+"\n\n" ;
}

}
Maciej Los 27-Sep-15 12:36pm    
Please, improve your question instead posting such of portion of code in a comment.
[no name] 27-Sep-15 12:27pm    
Most probably a little bit Overkill, but I strongly suggest to read it:
www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf[^]
aloweew 27-Sep-15 12:42pm    
how to add such thing like Delimiters whitespace-tilde-whitespace " ~ "?
String[] attributes = line.split(" ~ ");
in my code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900