SlideShare a Scribd company logo
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<double []> clusterCentroids;
Map<Integer,List<Double>> dataMatrix;
List<double[]> data;
Integer numClusters;
public void writeFile(List<Double> 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<double []>();
dataMatrix = new LinkedHashMap<>();
}
void readData(String filePath, int n, List<Double> 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<Double> 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<Double> centroids = new ArrayList<>();
String filePath = ("C:Usersmamdoeclipse-
workspaceK_projectsrcsynthetic_control_data.txt");
int n = 6;
for(int i=0;i<n;i++) {
Random r = new Random();
centroids.add((double)r.nextInt(600)+1);
}
System.out.println("Initial Clusters Centroids : " + centroids);
KMeans km = new KMeans();
//km.readData(filePath,n,centroids);
//km.calc();
}
}
Please I am trying to get this code to output in -txt file- I need you.pdf

More Related Content

PDF
please I need help- make sure its correct and I will give you ty Matc.pdf
PDF
please help-) Weekly Challenge- There are a lot of blood vessels- Howe (1).pdf
PDF
Please match the muscles with its correct bony origin- Responses can b.pdf
PDF
Please match the muscle with its correct insertion site- Biceps brachi.pdf
PDF
Please match the appropriate Career Readiness Competency with their de.pdf
PDF
Please make GUI in Eclipse Java Media Rental System Before attempting.pdf
PDF
please keep it short and simple Review Sulfide-Indole-Motility medium.pdf
PDF
Please help!! An igneous rock forms when melt solidifies- The rock c.pdf
please I need help- make sure its correct and I will give you ty Matc.pdf
please help-) Weekly Challenge- There are a lot of blood vessels- Howe (1).pdf
Please match the muscles with its correct bony origin- Responses can b.pdf
Please match the muscle with its correct insertion site- Biceps brachi.pdf
Please match the appropriate Career Readiness Competency with their de.pdf
Please make GUI in Eclipse Java Media Rental System Before attempting.pdf
please keep it short and simple Review Sulfide-Indole-Motility medium.pdf
Please help!! An igneous rock forms when melt solidifies- The rock c.pdf

More from asenterprisestyagi (20)

PDF
Please label and answer all questions please and explain what is 1-.pdf
PDF
Please use the same variables and only write the TODO part #!-usr-bi.pdf
PDF
Please use python to write a script for below- 1- Upload the audio (Pl.pdf
PDF
Please Use Java 8 for this project Humans VS Gob.pdf
PDF
Please solving using Alloy Add assort differentTransitionsForFSM- whic.pdf
PDF
please solve as soon as possible 1- In a Joint Liability Company the.pdf
PDF
Please solve c) and show code using Alloy make sure code can run Add f.pdf
PDF
please help- I will upvote- thank you! Q- RESPA specifies percentage r.pdf
PDF
please show work (by hand) Translate the following C code to MIPS ass.pdf
PDF
Please show work- Correct answer is given- Thanks 8- On December 31st.pdf
PDF
please solve all of the following What is a -metapopulation-- A group.pdf
PDF
Please show work- Correct answer is given- Thanks 5- Kettle Inc- was.pdf
PDF
Please select ALL the statements about inbreeding that are TRUE- A- In.pdf
PDF
Please show all the work 4- The nori instruction is not part of the M.pdf
PDF
Please help with this Case National Public Radio Many Voices Serving.pdf
PDF
please help with this Assignment Table below presents modal analysis.pdf
PDF
Please show all steps and do not recycle another answer 2- Prove the f.pdf
PDF
Please review the 6 steps to Emotional Intelligence video and the chap.pdf
PDF
Please select the three (3) correct answers- Firms pay out cash to inv.pdf
PDF
Please help with the powershell question- 2- A System creates a new u.pdf
Please label and answer all questions please and explain what is 1-.pdf
Please use the same variables and only write the TODO part #!-usr-bi.pdf
Please use python to write a script for below- 1- Upload the audio (Pl.pdf
Please Use Java 8 for this project Humans VS Gob.pdf
Please solving using Alloy Add assort differentTransitionsForFSM- whic.pdf
please solve as soon as possible 1- In a Joint Liability Company the.pdf
Please solve c) and show code using Alloy make sure code can run Add f.pdf
please help- I will upvote- thank you! Q- RESPA specifies percentage r.pdf
please show work (by hand) Translate the following C code to MIPS ass.pdf
Please show work- Correct answer is given- Thanks 8- On December 31st.pdf
please solve all of the following What is a -metapopulation-- A group.pdf
Please show work- Correct answer is given- Thanks 5- Kettle Inc- was.pdf
Please select ALL the statements about inbreeding that are TRUE- A- In.pdf
Please show all the work 4- The nori instruction is not part of the M.pdf
Please help with this Case National Public Radio Many Voices Serving.pdf
please help with this Assignment Table below presents modal analysis.pdf
Please show all steps and do not recycle another answer 2- Prove the f.pdf
Please review the 6 steps to Emotional Intelligence video and the chap.pdf
Please select the three (3) correct answers- Firms pay out cash to inv.pdf
Please help with the powershell question- 2- A System creates a new u.pdf
Ad

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Basic Mud Logging Guide for educational purpose
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Classroom Observation Tools for Teachers
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Lesson notes of climatology university.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Pharma ospi slides which help in ospi learning
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Basic Mud Logging Guide for educational purpose
PPH.pptx obstetrics and gynecology in nursing
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Computing-Curriculum for Schools in Ghana
Abdominal Access Techniques with Prof. Dr. R K Mishra
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Lesson notes of climatology university.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
GDM (1) (1).pptx small presentation for students
Renaissance Architecture: A Journey from Faith to Humanism
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pharma ospi slides which help in ospi learning
Supply Chain Operations Speaking Notes -ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Ad

Please I am trying to get this code to output in -txt file- I need you.pdf

  • 1. 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<double []> clusterCentroids; Map<Integer,List<Double>> dataMatrix; List<double[]> data; Integer numClusters; public void writeFile(List<Double> 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<double []>(); dataMatrix = new LinkedHashMap<>(); } void readData(String filePath, int n, List<Double> 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) {
  • 2. 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<Double> 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<Double> centroids = new ArrayList<>(); String filePath = ("C:Usersmamdoeclipse- workspaceK_projectsrcsynthetic_control_data.txt"); int n = 6; for(int i=0;i<n;i++) { Random r = new Random(); centroids.add((double)r.nextInt(600)+1); } System.out.println("Initial Clusters Centroids : " + centroids); KMeans km = new KMeans(); //km.readData(filePath,n,centroids); //km.calc(); } }