1

I am reading a CSV file that looks like the following:

    Red Blue Green
1st   Y    N     
2nd   Y    Y     N
3rd   N          Y

I want the output to be something like

1st Red Y
1st Blue N
2nd Red Y
2nd Blue Y
2nd Green N
3rd Red N
3rd Green Y

I am pulling in the colors row into an array, but I am not sure how to get my desired output. Below is my code so far:

public String readFile(File aFile) throws IOException {
    StringBuilder contents = new StringBuilder();
    ArrayList<String> topRow = new ArrayList<String>();

    try {
        BufferedReader input =  new BufferedReader(new FileReader(aFile));

        try {
            String line = null; 

        while (( line = input.readLine()) != null){
           if(line.startsWith(",")) {
              for (String retval: line.split(",")) {
                 topRow.add(retval);
                 //System.out.println(retval);

              }
           }
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }

    return contents.toString(); 
}
1
  • DO NOT REINVENT THE WHEEL. Use an existing, debugged CSV library (there are several). Commented Feb 24, 2017 at 17:45

1 Answer 1

1

The first row needs to be read and stored as array/list (I prefer array here, as it will be faster). Then subsequent rows needs to be parsed and stored, with the column name fetched from the first row, now stored as array.

In the code, I have directly written a String with line breaks, I suggest to use a List of String Array (of length 3), so that it can be used easily for any future action.

public String readFile(File aFile) throws IOException {

String data = "";

try {
    BufferedReader input =  new BufferedReader(new FileReader(aFile));
    String line = null;
    int cnt = 0;
    String[] topRow = new String[0]; 
    while (( line = input.readLine()) != null){
        if(cnt==0){
            String[] l = line.split(",");
            topRow = new String[l.length-1];
            for(int i= 0; i<l.length-1; i++){
                topRow[i] = l[i+1];
            }
         }
         else{
            String[] l = line.split(",");
            for(int i= 1; i<Math.min(l.length, topRow.length+1); i++){
                if(!l[i].equals("")){
                    String row = "";
                    row = l[0];
                    row = row + " " + topRow[i-1];
                    row = row + " " + l[i];
                    if(data.equals(""))data = row;
                    else data = data + "\n" + row;
                 }
              }
         }
         cnt++;
    }
}
catch (IOException ex){
  ex.printStackTrace();
}
return data; 

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.