-1

I have to read data from file Here is data and want plot a graph vs thick(column 1 in data) and alpha(column 3) for every model. Every model has 7 line data,the last that start with 0 not required. Here is my code. it works but i don't think it is good code.please, suggest me better way to do the same.

public class readFile {

public static int showLines(String fileName)  {
    String line;
    int currentLineNo = 0;
    BufferedReader in = null;
    try {
        in = new BufferedReader (new FileReader(fileName));

        //read until endLine
        while(((line = in.readLine()) != null)) {

            if (!line.contains("M") && !line.contains("#") && !line.trim().startsWith("0")) {
                //skipping the line that start with M, # and 0.
                currentLineNo++;
            } 
        }   
    } catch (IOException ex) {
        System.out.println("Problem reading file.\n" + ex.getMessage());
    } finally {
        try { if (in!=null) in.close(); } catch(IOException ignore) {}
    }
    return currentLineNo;
}

//Now we know the dimension of matrix, so storing data into matrix
public static void readData(String fileName,int numRow)  {
    String line;
    String temp []=null;
    String data [][]=new String[numRow][10];
    int i=0;


    BufferedReader in = null;
    try {
        in = new BufferedReader (new FileReader(fileName));

        //read until endLine
        while(((line = in.readLine()) != null)) {
            if (!line.contains("M") && !line.contains("#") && !line.trim().startsWith("0")) {

                    temp=(line.trim().split("[.]"));
                    for (int j = 0; j<data[i].length; j++) {    
                        data[i][j] =temp[j];
                    }
                    i++;
                }
            }
        // Extract one column from 2d matrix
        for (int j = 0; j <numRow; j=j+6) {
            for (int j2=j; j2 <6+j; j2++) {
                System.out.println(Double.parseDouble(data[j2][0])+"\t"+Double.parseDouble(data[j2][2]));  
                //6 element of every model, col1 and col3
                // will add to dataset.
            }
        }

    } catch (IOException ex) {
        System.out.println("Problem reading file.\n" + ex.getMessage());
    } finally {
        try { if (in!=null) in.close(); } catch(IOException ignore) {}
    }
}

//Main Method
public static void main(String[] args) {
    //System.out.println(showLines("rf.txt"));
    readData("rf.txt",showLines("rf.txt") );

}

}

Ranjeet
  • 322
  • 1
  • 4
  • 13

1 Answers1

0

as johnchen902 implies use a list

List<String> input=new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
    input.add(line);
}
br.close();
int N=input.get(0).split(",").size(); // here add your delimiter
int M=input.size();
String[][] data=new String[M][N]
for (int i=0;i<M;i++){
    String[] parts = string.split("-");
    for (int k=0;k<n;k++){
        data[i][k]=parts[k];
    }
}

something like that

hope it helps. plz put more effort into asking the question. Give us the needed Input files, and the Code you came up with until now to solve the problem yourself.

meister_reineke
  • 334
  • 1
  • 11
  • (1). Getting error in: int N=input.get(0).split(","); // error:mismatch: cannot convert from String[] to int (2). parks[0] will give 1st column of data so will this work:data[i][k]=parts[k]; – Ranjeet Feb 20 '15 at 17:52