In Java, you can use the java.io.BufferedReader
and java.util.StringTokenizer
classes to read and parse CSV (comma-separated values) files, and then compare the values in the CSV file.
Here is an example of how to read and parse a CSV file and compare the values in Java:
import java.io.BufferedReader; import java.io.FileReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("data.csv"))) { String line; while ((line = reader.readLine()) != null) { StringTokenizer tokenizer = new StringTokenizer(line, ","); String name = tokenizer.nextToken(); int age = Integer.parseInt(tokenizer.nextToken()); String gender = tokenizer.nextToken(); // Compare the values in the CSV file if (name.equals("John") && age == 30 && gender.equals("Male")) { System.out.println("Found a match!"); } } } catch (Exception e) { e.printStackTrace(); } } }
In this example, we use the BufferedReader
class to read the CSV file line by line, and the StringTokenizer
class to parse the values in each line. The StringTokenizer
class is used to split the line into tokens using the comma character as the delimiter. We then store the values in variables and compare them using the equals
and ==
operators.
Note that this example assumes that the CSV file is structured as follows:
Name,Age,Gender John,30,Male Alice,25,Female Bob,35,Male