Sure, here's the modified code that accepts input from the console instead of a file:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class KMeansClustering {
private static final double EPSILON = 1e-6;
// Data structure for storing a single data point
private static class Point {
double x;
double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
// Compute Euclidean distance between two points
private static double distance(Point p1, Point p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return Math.sqrt(dx * dx + dy * dy);
}
// Compute mean point of a cluster
private static Point mean(ArrayList<Point> cluster) {
double sumX = 0;
double sumY = 0;
for (Point p : cluster) {
sumX += p.x;
sumY += p.y;
}
int n = cluster.size();
return new Point(sumX / n, sumY / n);
}
public static void main(String[] args) {
// Parse data from input
Scanner scanner = new Scanner(System.in);
ArrayList<Point> data = new ArrayList<>();
System.out.print("Enter the number of data points: ");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
System.out.print("Enter the x and y coordinates separated by a comma: ");
String line = scanner.next();
String[] values = line.split(",");
double x = Double.parseDouble(values[0]);
double y = Double.parseDouble(values[1]);
data.add(new Point(x, y));
}
scanner.close();
// K-means clustering
int k = 3; // Number of clusters
ArrayList<Point>[] clusters = new ArrayList[k];
for (int i = 0; i < k; i++) {
clusters[i] = new ArrayList<>();
clusters[i].add(data.get(i)); // Initialize each cluster with a different data point
}
boolean converged = false;
while (!converged) {
// Assign each data point to the nearest cluster
for (Point p : data) {
int nearestCluster = 0;
double minDistance = distance(p, mean(clusters[0]));
for (int i = 1; i < k; i++) {
double distance = distance(p, mean(clusters[i]));
if (distance < minDistance) {
nearestCluster = i;
minDistance = distance;
}
}
clusters[nearestCluster].add(p); // Add the data point to the nearest cluster
}
// Compute new mean points of each cluster
Point[] newMeans = new Point[k];
for (int i = 0; i < k; i++) {
newMeans[i] = mean(clusters[i]);
}
// Check for convergence
converged = true;
for (int i = 0; i < k; i++) {
if (Math.abs(newMeans[i].x - mean(clusters[i]).x) > EPSILON ||
Math.abs(newMeans[i].y - mean(clusters[i]).y) > EPSILON) {
converged = false;
break;
}
}
// Reset clusters and update cluster means
for (int i = 0; i < k; i++) {
clusters[i] = new ArrayList<>();
clusters[i].add(newMeans[i]);
}
}
// Print results
for (int i = 0; i < k; i++) {
System.out.println("Cluster " + i + ": " + Arrays.toString(clusters[i].toArray()));
}
}
}
This code prompts the user for the number of data points and the x and y coordinates of each data point, separated by a comma. The input is read from the console using the Scanner class.
You can try running this code and providing input as follows:
Enter the number of data points: 6
Enter the x and y coordinates separated by a comma: 1,2
Enter the x and y coordinates separated by a comma: 2,3
Enter the x and y coordinates separated by a comma: 4,5
Enter the x and y coordinates separated by a comma: 5,6
Enter the x and y coordinates separated by a comma: 8,9
Enter the x and y coordinates separated by a comma: 9,10
The output should be the same as before.