Question: Please I am trying to get this code to output in .txt file: I need your help: import java.io.File; // Import the File class import
Please I am trying to get this code to output in .txt file:
I need your help:
import java.io.File; // Import the File class import java.io.FileNotFoundException; // Import this class to handle errors // Import the Scanner class to read text files import java.util.*;
import java.io.*; public class KMeans { List clusterCentroids; Map> dataMatrix; List data; Integer numClusters; public void writeFile(List value, File filename) { try { FileWriter myWriter = new FileWriter(filename); myWriter.write(value.toString()); myWriter.close(); System.out.println("Successfully wrote to the file."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public KMeans() { data = new ArrayList(); dataMatrix = new LinkedHashMap<>(); } void readData(String filePath, int n, List c) { numClusters = n; try { BufferedReader br = new BufferedReader(new FileReader(new File(filePath))); String line; int i = 0; while((line = br.readLine()) != null) { String [] elem = line.split("\\s+"); int j = 0; for(String d : elem) { if(isCentroid(j, c)) { this.clusterCentroids.get(i)[j] = ((Double.valueOf(d))); } //data.get(i)[j] = ((Double.valueOf(d))); System.out.println(data.get(j)[i]); } System.out.println(); i++; } br.close();
} catch (IOException ex) { ex.printStackTrace(); } } private boolean isCentroid(int j, List c) {
for(int i = 0; i < c.size(); i++) { if(j == c.get(i)) { return true; } } return false; } public static void main(String [] args) { Scanner sc = new Scanner(System.in); List centroids = new ArrayList<>(); String filePath = ("C:\\Users\\mamdo\\eclipse-workspace\\K_project\\src\\synthetic_control_data.txt"); int n = 6; for(int i=0;i