3

I want to be able to loop through the second column of a csv file and find the indexes of a particular string. So for example find all the indexes with the value "Chelsea". Below is the code I have so far.

Any Help?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CSVReader {

    public static void main(String[] args) {
        CSVReader obj = new CSVReader();
        obj.run();
    }

    public void run() {
        String csv = "2015:2016.csv";
        BufferedReader br = null;
        String line = "";
        String csvSplit = ",";
        String[] football = new String[0];
        try {
            br = new BufferedReader(new FileReader(csv));
            String headerLine = br.readLine();

            while ((line = br.readLine()) != null) {
                football = line.split(csvSplit);
            }
        }
        catch (IOException io) {
            System.out.println(io);
        }
    }
}
2
  • 1
    football[1] will give you second column. Also, you will probably need a loop or an even better approach (like reading all the lines at once using the Files.readAllLines() Commented May 31, 2016 at 18:09
  • 1
    Files.readAllLines()will load all your file into memory so make sure that the file will never be big before using this appraoch Commented May 31, 2016 at 18:12

2 Answers 2

2

In terms of finding Chelsea, you are only an if statement away at minimum. Merge this code into your while loop:

while ((line = br.readLine()) != null) {
    football = line.split(csvSplit);
    if(football[1].equals("Chelsea") {
        System.out.println("I found Chelsea!");
    }
}

Of course, I imagine you'll want to be a little informative than that. If you wanted to provide a line number for the line Chelsea was on, you could do something like:

int index = 1;
while ((line = br.readLine()) != null) {
    football = line.split(csvSplit);
    if(football[1].equals("Chelsea") {
        System.out.println("I found Chelsea on line " + index);
    }
    index++;
}

Alternatively, if you want to work with one or all elements of Chelsea's row, you could do something like:

while ((line = br.readLine()) != null) {
    football = line.split(csvSplit);
    if(football[1].equals("Chelsea") {
        System.out.println("I found Chelsea on row with first column equal to " + football[0]);
        System.out.println("The entire row consists of: ");
        for(int i = 0; i < football.length; i++) {
            System.out.print(football[i] + ", ");
        }
        System.out.println();
    }
}

Or you can feed Chelsea's line to another part of your program to do other fancy things with it.

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

1 Comment

Great just what I needed! Thank you!
1

Here is a solution that is a bit different from your solution but does what you want.

public class Main {

public static void main(String[] args) throws FileNotFoundException, IOException {

    BufferedReader bfr = new BufferedReader(new FileReader("path_2_csv_file"));

    List<Integer> results = new ArrayList<>();
    String  l= "";
    int index = 1;
    while((l = bfr.readLine()) != null){

        //break each line of the csv file to its elements
        String[] elements=l.split(",");

        //check if the second column is equal to e.g. Chelsea
        if(elements[1].equals("Chelsea")){
            results.add(index);
        }

        index++;
    }

    //print the results
    for(Integer i : results){
        System.out.println(i);
    }
  }
}

3 Comments

Hi Meghi, okay great thanks, once I find the indexes, is there anyway to use these indexes to find the corresponding value in the next column?
Yes. Create a List<String[]> allLines = new ArrayList<String[]>(). At each iteration of the while loop store the line in this List. allLines.add(l.split(",")).
Then you can access the desired index (line) by allLines.get(i-1).

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.