I have 2 words ("Jakarta", "Bandung" ) in a CSV file that i want to retrieve and put it in the combobox. I can get those words, but based on my code, i will get every Jakarta and Bandung word, while i only want to retrieve it once per word. And i tried to put it in the combobox, but somehow it won't show up. This is my code so far.
public int a;
public String location;
public String destination;
private void formWindowOpened(java.awt.event.WindowEvent evt) {
String csvFile = "C:\\Users\\USER\\Desktop\\Project Data.csv";
BufferedReader br = null;
LineNumberReader reader = null;
String line = "";
String cvsSplitBy = ",";
br = new BufferedReader(new FileReader(csvFile));
reader = new LineNumberReader(new FileReader(csvFile));
while ((reader.readLine()) != null);{
a = reader.getLineNumber();
}
reader.close();
while ((line = br.readLine()) != null) {
String[] data = line.split(cvsSplitBy);
location = data[1];
destination = data[2];
}
int i;
for(i = 0; i < a; i++){
if(location.equals("Jakarta")){
cmb1.addItem("Jakarta");
}
}
Edit :
i tried another method using scanner, still no luck. Here is the code
String csvFile = "C:\\Users\\USER\\Desktop\\Project Data.csv";
Scanner sc = new Scanner(new File(csvFile));
while(sc.hasNext()){
String word = sc.next();
if(word.equalsIgnoreCase("Jakarta")){
cmb1.addItem("Jakarta");
JOptionPane.showMessageDialog(null, "Jakarta");
break;
}
}