On Java Ive make a simple program with search bar. Ive also a CSV File 'file.csv' that contains this:
"ID","FIRSTNAME","LASTNAME"
"JM1","Jean","Martial"
"AD1","Audrey","Dubois"
"BX1","Bertrand","Xavier"
I can open the file on Java with this line.
String file = "C:\\file.csv";
To verify if file exists I use this line.
if(new File(file).exists()) {
JOptionPane.showMessageDialog(frame, "Fichier ouvert succes");
}
Now I want to parse the file to extract AD1 and display true if exist or false if not exists. Ive declared Scanner for this, but i dont know how to setup for this.
Scanner scanner = null;
try {
scanner = new Scanner(new File(file));
scanner.useDelimiter(coma_delimiter);
while(scanner.hasNext()) {
String s1= scanner.next();
System.out.print(s1 +" ");
if(s1.equals(search_field.getText())) {
System.out.print("OKOK");
} else {
System.out.println("NOK");
}
}
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} finally {
scanner.close();
}
Here the search_field is a JTextField.
String file = "C:\\file.csv";" - no, that is just a string and doesn't open the file at all. Besides that, if you are able to read the file line by line then what's the problem with checking whether the line contains ADB1? (Note that you don't want to check usingequals()butcontains()). If you need to do more then it might be better to look for a CSV parser library and use that.