Hi, I'm trying to remove duplicates from an ArrayList made from tokens (from cvs file) using Scanner method. The file output from my code does not remove duplicates!
import java.io.*; import java.util.*; public class removeDuplicates { public static void main (String[] args) throws IOException { String line = ""; StringBuilder addToFile = new StringBuilder(); try{ Scanner myFile = new Scanner(new File("data.csv")); File output = new File("correctedData.cvs"); while (file.hasNext()){ line = file.nextLine(); String[] tokens = line.split(","); List<String> List = new ArrayList<>(); List.add(tokens[3]); List<String> newList = new ArrayList<>(); for (String x : List){ if (!newList.contains(x)) { newList.add(x); } } addToFile.append("\n").append(newList); } file.close(); PrintWriter fileSave = new PrintWriter(output); fileSave.println(addToFile); fileSave.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
MY OUTPUT:
[mumbai]
[london]
[paris]
[london]
Duplicates not removed...