Naf Rash

Ranch Hand

Posts: 85

posted 13 years ago

Hi there. Just a quick question I keep getting the following error but don't know why-as far as I can see the code is fine but knowing me i'm probably wrong.

I don't think you actually need the whole code but just in case I have provided it all. The error appears after the first few lines of the code in line 9:

private Vector euclidean(ResultSetMetaData rsmd){

The error is: Syntax error on token "(", ";" expected - the error is in relation to the opening bracket ie, after the word euclidean.

Anyhow here's the class!

import java.sql.*;

import java.util.*;

public class distances {

public static void main(String args[]) {

/*This method calculates the distances between records using Euclidean distance*/

private Vector euclidean(ResultSetMetaData rsmd){

//ResultSetMetaData rsmd;

int j = rsmd.getRowCount();

int l = rsmd.getColumnCount();

double distance;

int [][]sum;

sum = new int [j][l];

int theSum[];

theSum = new int [l];

/* Comparing each record attribute with the next record. The sum stores the value of the

* differences between records (based on single attributes). The first for stmt looks at

* one row at a time- the second for stmt the compares this row to all the other rows.

* For every row comparison all the attributes are considered. NOTE: the second for stmt

* starts at 2 so that the second row is considered (the first row is dealt with in the

* first for stmt).*/

for (int i=1; i<j; ++i){

for (int all = 2; all<j; ++all){

for (int k=1; k<l; ++k)//attributes

/* Carry out the calculations to work out the euclidean distance. If the attribute value being

* considered of type int then an exception occurrs and is caught in the catch clause- if the

* 2 values differ then the distace is equal to 1 else it is equal to 0.*/

try{

for (int c=1; c<l; ++c){

theSum[c]=(sum[i][k] - sum[all][k]);

}

/*Returns the squared result of the difference found between different record for the same

* attribute.*/

for (int m=0; m<theSum.length; ++m )//need to square each element of theSum.

theSum[m] = theSum[m]*theSum[m];

/*Add all the attribute differences together and then square them. Stores the resulting values

* in an array. */

for (int ts=0; ts<theSum.length-1; ++ts){

int tts[] = tts + sum [ts];

distance = Math.sqrt((double)tts[ts]);//get square root of tts

}

}//end try

catch (NumberFormatException nfe){

if (sum[i][k] == sum[all][k]){

distance = 0;

}

else if (sum[i][k] == sum[all][k]){

distance =1;

}

}//end catch

/*Devise the matrix. For the tuples being considered ther Squared Euclidean distance is entered in the

* corresponding element of the array.This produces a matrix with the same number of column as rows.

* The size of the matrix changes since it stores some calculations that

* have been carried out on database rows. As a result the number of rows

* that the database has is called for and this determines the size of the

* matrix.*/

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();

double [][] distanceMatrix;

distanceMatrix= new double [row1][row2];

for (int i=1;i<row1; ++i){

for (int all = 1; all<row2; ++row2)

distanceMatrix[i][all] =distance;

}

}

}

}

private Vector squaredEuclidean (ResultSetMetaData rsmd) {

//ResultSetMetaData rsmd;

int j = rsmd.getRowCount();

int l = rsmd.getColumnCount();

double distance;

int [][]sum;

sum = new int [j][l];

int theSum[];

theSum = new int [l];

/* Comparing each record attribute with the next record. The sum stores the value of the

* differences between records (based on single attributes). The first for stmt looks at

* one row at a time- the second for stmt the compares this row to all the other rows.

* For every row comparison all the attributes are considered. NOTE: the second for stmt

* starts at 2 so that the second row is considered (the first row is dealt with in the

* first for stmt).*/

for (int i=1; i<j; ++i){

for (int all = 2; all<j; ++all){

for (int k=1; k<l; ++k)//attributes

/* Carry out the calculations to work out the euclidean distance. If the attribute value being

* considered of type int then an exception occurrs and is caught in the catch clause- if the 2

* values differ then the distace is equal to 1 else it is equal to 0.*/

try{

for (int c=1; c<l; ++c){

theSum[c]=(sum[i][k] - sum[all][k]);

}

/*Returns the squared result of the difference found between different record for the same

* attribute.*/

for (int m=0; m<theSum.length; ++m )//need to square each element of theSum.

theSum[m] = theSum[m]*theSum[m];

/*Add all the attribute differences together and then square them. Stores the resulting values

* in an array. */

for (int ts=0; ts<theSum.length-1; ++ts){

distance = distance + theSum [ts];

}

}//end try

catch (NumberFormatException nfe){

if (sum[i][k] == sum[all][k]){

distance = 0;

}

else if (sum[i][k] == sum[all][k]){

distance =1;

}

}//end catch

/*Devise the matrix. For the tuples being considered ther Squared Euclidean distance is entered in the

* corresponding element of the array.This produces a matrix with the same number of column as

* rows. The size of the matrix changes since it stores some calculations that have been

* carried out on database rows. As a result the number of rows that the database has is called

* for and this determines the size of the matrix.*/

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();

double[][] distanceMatrix;

distanceMatrix= new double [row1][row2];

for ( i=1;i<row1; ++i){

for (int all = 1; all<row2; ++row2)

distanceMatrix[i][all] =distance;

}

}

}

}

private Vector manhattan(ResultSetMetaData rsmd) {

//ResultSetMetaData rsmd;

int j = rsmd.getRowCount();

int l = rsmd.getColumnCount();

double distance;

int [][]sum;

sum = new int [j][l];

int theSum[];

theSum = new int [l];

/* Comparing each record attribute with the next record. The sum stores the value of the

* differences between records (based on single attributes). The first for stmt looks at

* one row at a time- the second for stmt the compares this row to all the other rows.

* For every row comparison all the attributes are considered. NOTE: the second for stmt

* starts at 2 so that the second row is considered (the first row is dealt with in the

* first for stmt).*/

for (int i=1; i<j; ++i){

for (int all = 2; all<j; ++all){

for (int k=1; k<l; ++k)//attributes

/* Carry out the calculations to work out the manhattan distance. If the attribute value being

* considered of type int then an exception occurrs and is caught in the catch clause- if the 2

* values differ then the distace is equal to 1 else it is equal to 0.*/

try{

for (int c=1; c<l; ++c){

theSum[c]=(sum[i][k] - sum[all][k]);

}

/*Add all the attribute differences together and then square them. Stores the resulting values

* in an array. */

for (int ts=0; ts<theSum.length-1; ++ts){

distance = distance + theSum [ts];

}

//check to see if the result of the calculation is negative. If so, make it positive.

if (distance<0){

distance = distance*distance;

}

}

catch (NumberFormatException nfe){

if (sum[i][k] == sum[all][k]){

distance = 0;

}

else if (sum[i][k] == sum[all][k]){

distance =1;

}

}//end catch

/*Devise the matrix. For the tuples being considered ther manhattan distance is entered in the

* corresponding element of the array.This produces a matrix with the same number of column as rows.

* The size of the matrix changes since it stores some calculations that

* have been carried out on database rows. As a result the number of rows

* that the database has is called for and this determines the size of the

* matrix.*/

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();

double [][] distanceMatrix;

distanceMatrix= new double [row1][row2];

for (i=1;i<row1; ++i){

for (all = 1; all<row2; ++row2)

distanceMatrix[i][all] =distance;

}

}

}

}

}

}//ends class

I don't think you actually need the whole code but just in case I have provided it all. The error appears after the first few lines of the code in line 9:

private Vector euclidean(ResultSetMetaData rsmd){

The error is: Syntax error on token "(", ";" expected - the error is in relation to the opening bracket ie, after the word euclidean.

Anyhow here's the class!

import java.sql.*;

import java.util.*;

public class distances {

public static void main(String args[]) {

/*This method calculates the distances between records using Euclidean distance*/

private Vector euclidean(ResultSetMetaData rsmd){

//ResultSetMetaData rsmd;

int j = rsmd.getRowCount();

int l = rsmd.getColumnCount();

double distance;

int [][]sum;

sum = new int [j][l];

int theSum[];

theSum = new int [l];

/* Comparing each record attribute with the next record. The sum stores the value of the

* differences between records (based on single attributes). The first for stmt looks at

* one row at a time- the second for stmt the compares this row to all the other rows.

* For every row comparison all the attributes are considered. NOTE: the second for stmt

* starts at 2 so that the second row is considered (the first row is dealt with in the

* first for stmt).*/

for (int i=1; i<j; ++i){

for (int all = 2; all<j; ++all){

for (int k=1; k<l; ++k)//attributes

/* Carry out the calculations to work out the euclidean distance. If the attribute value being

* considered of type int then an exception occurrs and is caught in the catch clause- if the

* 2 values differ then the distace is equal to 1 else it is equal to 0.*/

try{

for (int c=1; c<l; ++c){

theSum[c]=(sum[i][k] - sum[all][k]);

}

/*Returns the squared result of the difference found between different record for the same

* attribute.*/

for (int m=0; m<theSum.length; ++m )//need to square each element of theSum.

theSum[m] = theSum[m]*theSum[m];

/*Add all the attribute differences together and then square them. Stores the resulting values

* in an array. */

for (int ts=0; ts<theSum.length-1; ++ts){

int tts[] = tts + sum [ts];

distance = Math.sqrt((double)tts[ts]);//get square root of tts

}

}//end try

catch (NumberFormatException nfe){

if (sum[i][k] == sum[all][k]){

distance = 0;

}

else if (sum[i][k] == sum[all][k]){

distance =1;

}

}//end catch

/*Devise the matrix. For the tuples being considered ther Squared Euclidean distance is entered in the

* corresponding element of the array.This produces a matrix with the same number of column as rows.

* The size of the matrix changes since it stores some calculations that

* have been carried out on database rows. As a result the number of rows

* that the database has is called for and this determines the size of the

* matrix.*/

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();

double [][] distanceMatrix;

distanceMatrix= new double [row1][row2];

for (int i=1;i<row1; ++i){

for (int all = 1; all<row2; ++row2)

distanceMatrix[i][all] =distance;

}

}

}

}

private Vector squaredEuclidean (ResultSetMetaData rsmd) {

//ResultSetMetaData rsmd;

int j = rsmd.getRowCount();

int l = rsmd.getColumnCount();

double distance;

int [][]sum;

sum = new int [j][l];

int theSum[];

theSum = new int [l];

/* Comparing each record attribute with the next record. The sum stores the value of the

* differences between records (based on single attributes). The first for stmt looks at

* one row at a time- the second for stmt the compares this row to all the other rows.

* For every row comparison all the attributes are considered. NOTE: the second for stmt

* starts at 2 so that the second row is considered (the first row is dealt with in the

* first for stmt).*/

for (int i=1; i<j; ++i){

for (int all = 2; all<j; ++all){

for (int k=1; k<l; ++k)//attributes

/* Carry out the calculations to work out the euclidean distance. If the attribute value being

* considered of type int then an exception occurrs and is caught in the catch clause- if the 2

* values differ then the distace is equal to 1 else it is equal to 0.*/

try{

for (int c=1; c<l; ++c){

theSum[c]=(sum[i][k] - sum[all][k]);

}

/*Returns the squared result of the difference found between different record for the same

* attribute.*/

for (int m=0; m<theSum.length; ++m )//need to square each element of theSum.

theSum[m] = theSum[m]*theSum[m];

/*Add all the attribute differences together and then square them. Stores the resulting values

* in an array. */

for (int ts=0; ts<theSum.length-1; ++ts){

distance = distance + theSum [ts];

}

}//end try

catch (NumberFormatException nfe){

if (sum[i][k] == sum[all][k]){

distance = 0;

}

else if (sum[i][k] == sum[all][k]){

distance =1;

}

}//end catch

/*Devise the matrix. For the tuples being considered ther Squared Euclidean distance is entered in the

* corresponding element of the array.This produces a matrix with the same number of column as

* rows. The size of the matrix changes since it stores some calculations that have been

* carried out on database rows. As a result the number of rows that the database has is called

* for and this determines the size of the matrix.*/

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();

double[][] distanceMatrix;

distanceMatrix= new double [row1][row2];

for ( i=1;i<row1; ++i){

for (int all = 1; all<row2; ++row2)

distanceMatrix[i][all] =distance;

}

}

}

}

private Vector manhattan(ResultSetMetaData rsmd) {

//ResultSetMetaData rsmd;

int j = rsmd.getRowCount();

int l = rsmd.getColumnCount();

double distance;

int [][]sum;

sum = new int [j][l];

int theSum[];

theSum = new int [l];

/* Comparing each record attribute with the next record. The sum stores the value of the

* differences between records (based on single attributes). The first for stmt looks at

* one row at a time- the second for stmt the compares this row to all the other rows.

* For every row comparison all the attributes are considered. NOTE: the second for stmt

* starts at 2 so that the second row is considered (the first row is dealt with in the

* first for stmt).*/

for (int i=1; i<j; ++i){

for (int all = 2; all<j; ++all){

for (int k=1; k<l; ++k)//attributes

/* Carry out the calculations to work out the manhattan distance. If the attribute value being

* considered of type int then an exception occurrs and is caught in the catch clause- if the 2

* values differ then the distace is equal to 1 else it is equal to 0.*/

try{

for (int c=1; c<l; ++c){

theSum[c]=(sum[i][k] - sum[all][k]);

}

/*Add all the attribute differences together and then square them. Stores the resulting values

* in an array. */

for (int ts=0; ts<theSum.length-1; ++ts){

distance = distance + theSum [ts];

}

//check to see if the result of the calculation is negative. If so, make it positive.

if (distance<0){

distance = distance*distance;

}

}

catch (NumberFormatException nfe){

if (sum[i][k] == sum[all][k]){

distance = 0;

}

else if (sum[i][k] == sum[all][k]){

distance =1;

}

}//end catch

/*Devise the matrix. For the tuples being considered ther manhattan distance is entered in the

* corresponding element of the array.This produces a matrix with the same number of column as rows.

* The size of the matrix changes since it stores some calculations that

* have been carried out on database rows. As a result the number of rows

* that the database has is called for and this determines the size of the

* matrix.*/

int row1= rsmd.getRowCount(), row2= rsmd.getRowCount();

double [][] distanceMatrix;

distanceMatrix= new double [row1][row2];

for (i=1;i<row1; ++i){

for (all = 1; all<row2; ++row2)

distanceMatrix[i][all] =distance;

}

}

}

}

}

}//ends class

Dan Lastoria

Ranch Hand

Posts: 57

Layne Lund

Ranch Hand

Posts: 3061

posted 13 years ago

So the solution is to move the euclidean() method outside of main(), but keep it inside the distances class.

As a suggestion, you should change distances to

Layne

As a suggestion, you should change distances to

**D**istances. Typically, class names start with upper-case letters.Layne