jooookra
jooookra

Reputation: 53

How to read CSV data by matching the input in java?

Trying to create a program where the user is asked to enter a location, if the user enters i.e south it can read from the "Location" column in the csv, and whatever row contains "south" (upper or lowercase accepted), the name(s) from the Name column are printed. So if the user enters south, Holos and Deafer are returned.

I can't seem to find how to actually print specific rows that contains matching input, and have only been able to print the whole column.

Any help is much appreciated!

Csv

Name, Location
Holos, South
Tredies, North
Warren, West
Deafer, South

Current code that only reads specific column

    public void filter() {
        // using buffered reader to read the csv file, still a WIP
        String path = "E:\\IT\\2022\\Further Programming\\s3902169_furtherProgrammingA1\\src\\files\\Melbnb.csv";
        String line = "";

        System.out.println("Enter location");
        String stringInput = readUserInput();

        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(path));
            while ((line = br.readLine()) != null) {
                String[] values = line.split(",");

                if (stringInput.contains(values[1])) {
                    System.out.print(values[0]);
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Upvotes: 0

Views: 582

Answers (1)

Nantha Kumar
Nantha Kumar

Reputation: 29

if (stringInput.equalsIgnoreCase(values[1)) {
        System.out.println(values[0]);
 }

Upvotes: 0

Related Questions