public class kmeansgeneral {
public static void main(final String args[]) throws Exception {
int words = 0;
int chars = 0;
int lines = 0;
String s, sp1;
StringTokenizer st;
final ArrayList<Double> x = new ArrayList<Double>();
final FileReader fr = new FileReader("G:/t1.txt");
final BufferedReader buf = new BufferedReader(fr);
// Counting number of words and lines
while ((s = buf.readLine()) != null) {
lines++;
st = new StringTokenizer(s, " ,;:.");
while (st.hasMoreTokens()) {
words++;
s = st.nextToken();
chars += s.length();
final Double y = new Double(s);
x.add(y);
}
}
System.out.println("Word Count : " + words / lines);
System.out.println("Line Count : " + lines);
// Counting and printing number of words and lines ENDS
Double ct[] = new Double[0];
ct = x.toArray(ct);
// Input array, values to be read in successively, float
// double[][] indat = new double[lines][lines*words];
final double[][] indat = new double[10][10];
int inval = 0;
final BufferedReader buf1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter K-Value : ");
sp1 = buf1.readLine();
final Integer ky = new Integer(sp1);
final int k = ky;
System.out.println("K == " + k);
// Now read in input array values, successively
for (int i = 0; i < lines; i++) {
for (int j = 0; j < words / lines; j++) {
indat[i][j] = ct[inval];
inval++;
System.out.print(indat[i][j]);
System.out.print("\t");
}
System.out.println();
}
// Initial Clusters
System.out.println(" ");
System.out.println(k + " seed points ");
final double[][] Clusters = new double[k][lines * words];
// double[][] calcnt = new double[lines][words];
final double[][] calcnt = new double[1000][1000];
final double[][] array = new double[k][lines * words];
System.out.println("Clusters==>");
// int pos=0;
for (int i = 0; i < k; i++) {
for (int j = 0; j < words / lines; j++) {
Clusters[i][j] = indat[i][j];
// pos= j;
System.out.print(Clusters[i][j]);
System.out.print("\t");
}
System.out.println();
}
System.out.println("PRINTING VECTOR ELEMENTS:");
final Vector FinalClusters[][] = new Vector[100][100];
for (int i = 0; i < k; i++) {
for (int j = 0; j < words / lines; j++) {
final String tempString = String.valueOf(Clusters[i][j]);
FinalClusters[i][j].add(tempString);
}
}
// Inital Cluster Array
System.out.println("Initial Cluster Array");
int b = 0;
final double[] arr = new double[2000];
for (int i = 0; i < k; i++) {
for (int j = 0; j < words / lines; j++) {
arr[b] = Clusters[i][j]; // = indat[i][j];
System.out.print(arr[b]);
System.out.print("\t");
b++;
}
}
System.out.println();
System.out.println("Centroids");
for (int i = 0; i < k; i++) {
for (int j = 0; j < words / lines; j++) {
calcnt[i][j] = (Clusters[i][j] + indat[k][j]) / 2;
System.out.print(calcnt[i][j]);
System.out.print("\t");
}
}
// Claculate Distances
// System.out.println("MAGIC # 3");
final double[] dist = new double[k];
for (int i = 0; i < k; i++) {
double dis = 0;
for (int j = 0; j < words / lines; j++) {
dis = dis + (Math.pow(Clusters[i][j] - indat[k][j], 2));
}
dist[i] = (Math.sqrt(dis));
System.out.println("From Cluster K = " + i + "\t" + "Distance" + dist[i]);
}
System.out.println("To Find Minimum Distance ");
double min = dist[0];
int y = 0;
for (int m = 0; m < k; m++) {
if (dist[m] < min) {
min = dist[m];
y = m;
}
}
System.out.print("Min Value =" + min + "\t" + "For Cluster :" + y);
System.out.println();
System.out.print("Added Cluster =");
final double[] temp = new double[lines * (words / lines)];
for (int j = 0; j < words / lines; j++) {
temp[j] = indat[k][j];
System.out.print(temp[j]);
System.out.print("\t");
}
System.out.println();
final Vector[] vector = new Vector[k];
for (int i = 0; i < k; i++) {
vector[i] = new Vector<Object>();
}
for (int i = 0; i < words / lines; i++) {
vector[y].add(String.valueOf(temp[i]));
}
System.out.println(Arrays.toString(vector));
}
}
答案 0 :(得分:2)
首先,您应该重新发布帖子,以便更清楚问题是什么。
然后删除所有不相关的代码。
然后看看你的堆栈跟踪,它应该说明哪一行是错的,你应该发布的那一行。
我想它是带有**的那个,否则你的代码将无法编译
所以我假设它的这一行:
FinalClusters[i][j].add(tempString);//is it this line ?
// where did you initialise FinalClusters[i][j] ??
// maybe you first need FinalClusters[i][j] = new Vector();
如果是,那么你的错误就是你确实启动了数组而不是数组中的每个元素。
旁注: 你真的需要那个数组吗?
答案 1 :(得分:2)
我在以下输入文件上运行了您的代码:
t1.txt:
123
234
345
34456
输出:
Word Count : 1
Line Count : 4
Enter K-Value : 3
K == 3
123.0
234.0
345.0
34456.0
3 seed points
Clusters==>
123.0
234.0
345.0
PRINTING VECTOR ELEMENTS:
Exception in thread "main" java.lang.NullPointerException
at test.kmeansgeneral.main(kmeansgeneral.java:106)
这就是这段代码:
System.out.println("PRINTING VECTOR ELEMENTS:");
final Vector FinalClusters[][] = new Vector[100][100];
for (int i = 0; i < k; i++) {
for (int j = 0; j < words / lines; j++) {
final String tempString = String.valueOf(Clusters[i][j]);
// NPE OCCURS IN THE LINE BELOW
FinalClusters[i][j].add(tempString);
}
}
问题是数组'FinalClusters'包含空值,而不是空Vector。所以这是一个修复,在发生错误的行上方添加以下行:
if (FinalClusters[i][j] == null) {
FinalClusters[i][j] = new Vector();
}
然后输出是:
Word Count : 1
Line Count : 4
Enter K-Value : 3
K == 3
123.0
234.0
345.0
34456.0
3 seed points
Clusters==>
123.0
234.0
345.0
PRINTING VECTOR ELEMENTS:
Initial Cluster Array
123.0 234.0 345.0
Centroids
17289.5 17345.0 17400.5 From Cluster K = 0 Distance34333.0
From Cluster K = 1 Distance34222.0
From Cluster K = 2 Distance34111.0
To Find Minimum Distance
Min Value =34111.0 For Cluster :2
Added Cluster =34456.0
[[], [], [34456.0]]
我不知道应该做什么,所以无法判断这是否正确。至少不会抛出任何异常。
答案 2 :(得分:0)
让我猜一下:你可能会在eclipse或某些编辑器中调试此代码时出现此错误,该编辑器无法为jvm提供控制台。
如果是这种情况,那么你可能会在sp1 = buf1.readLine();