View Single Post
  #1 (permalink)  
Old 04-19-2008, 10:20 PM
BHCluster BHCluster is offline
Member
 
Join Date: Mar 2008
Posts: 32
BHCluster is on a distinguished road
Can't solve error message while looping
Finally after few posts and few attempts I have managed to read text file and out put it on the screen. The file has 60 lines of data in three columns. Every thing works until I try to read 1 row of three doubles for calculation. So when I had this:

Code:
double[] gene={-9.30, 5.65, -5.02}; double[] gene1={-5.30, 3.65, -2.02};
it worked perfect but when I have tryed to read gen1 from the file with the use of errors I get this error:

Code:
G:\Read File\csv.java:145: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (java.lang.String[]) double ii = Double.parseDouble(myarray); ^ 1 error Tool completed with exit code 1
If anybody know what the problem is please help me cos I am running out of ideas. Here is the full program:
Code:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.Writer; import static java.lang.Math.*; public class csv { public static void Manhattan(double[][] a,double[][] b,int n,int n2) { int q=n2; int c=n; for(int m=0;m<q;m++) { for(int h=0;h<q;h++) { double d=0; double d2=0; for(int j=0;j<c;j++) { d2=a[m][j]-a[h][j]; if(d2<0) d2=d2*-1; d=d+d2; } b[m][h]=d; } } } public static double Euclidean(double[] array1, double[] array2) { //Store the numbers of the both arrays double x1= array1[0]; double y1=array2[0]; double x2= array1[1]; double y2= array2[1]; double x3=array1[2]; double y3=array2[2]; //Compute the minus part of the formula double minusResult1 = y1-x1; double minusResult2 = y2-x2; double minusResult3 = y3-x3; //Square the answers double square1 = pow(minusResult1, 2); double square2 = pow(minusResult2, 2); double square3 = pow(minusResult3, 2); //Sum the squares double sumOfSquares = square1+square2+square3; //Get the square root of the sumOfSquares double finalResult = sqrt(sumOfSquares); return finalResult; } public static double Manhattan(double[] array1, double[] array2) { //Store the numbers of the both arrays double x1= array1[0]; double y1=array2[0]; double x2= array1[1]; double y2= array2[1]; double x3=array1[2]; double y3=array2[2]; //Compute the minus part of the formula double minusResult1 = y1-x1; double minusResult2 = y2-x2; double minusResult3 = y3-x3; //Square the answers double abs1 = abs(minusResult1 ); double abs2 = abs(minusResult2); double abs3 = abs(minusResult3 ); //Sum the squares double sumOfAbs = abs1+abs2+abs3; //Get the square root of the sumOfAbs double finalResult = sqrt(sumOfAbs); return finalResult; } public static double Pearson(double[] array1, double[] array2) { //Store the numbers of the both arrays double x1= array1[0]; double y1=array2[0]; double x2= array1[1]; double y2= array2[1]; double x3=array1[2]; double y3=array2[2]; //Compute the minus part of the formula double mean1 = (x1 + x2 + x3)/3; double mean2 = (y1 + y2 + y3)/3; double t = (x1 - mean1)*(y1 - mean2) + (x2 - mean1)*(y2 - mean2) + (x3 - mean1)*(y3 - mean2); //Square the answers double square1 = sqrt((x1 - mean1)*(x1 - mean1) + (x2 - mean1)*(x2 - mean1) + (x3 - mean1)*(x3 - mean1)); double square2 = sqrt((y1 - mean2)*(y1 - mean2) + (y2 - mean2)*(y2 - mean2) + (y3 - mean2)*(y3 - mean2)); //Get the square root of the sumOfSquares double finalResult = t/(square1*square2); return finalResult; } public static void main(String[] args) throws Exception { // Set file name & path String filepath = "Data_Set_1.txt"; //String output = "Output_Data_Set_1.txt"; // Read in file FileInputStream in = new FileInputStream(filepath); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Write file //FileOutputStream out = new FileOutputStream(output); //BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(out)); // Declare Array which will hold 61 lines String[] myarray; myarray = new String[61]; double ii = Double.parseDouble(myarray); // Read each line into the array for (int i = 0; i < myarray.length; i++){ myarray[i] = br.readLine(); } in.close(); // Print out array info in desired order. Delete " characters for (int i = 1; i < myarray.length; i++){ System.out.println(myarray[i].replace("\"", "")); } double[] gene={-9.30, 5.65, -5.02}; double[] gene1={ii}; for (int i = 1; i < myarray.length; i++){ double EuclideanDist = Euclidean(gene, gene1); System.out.println("Euclidean distance = " + EuclideanDist); double ManhattanDist = Manhattan(gene, gene1); System.out.println("Manhattan = " + ManhattanDist); double PearsonDist = Pearson(gene, gene1); System.out.println("Pearson = " + PearsonDist); } } }
Text file looks like this:

Code:
-9.30 5.65 -5.02 -4.10 5.68 0.56 4.75 0.11 2.86 -1.54 -10.01 -1.99 -6.29 5.62 -2.78 -13.02 1.31 1.61 -5.85 3.79 -6.56 -12.71 -0.45 0.62 -3.05 -12.65 -5.07 2.18 -5.82 -9.73 1.74 -0.99 -3.66 -7.88 0.93 -8.33 -6.34 -1.85 -4.00
Thank you!
Reply With Quote
Sponsored Links