View Single Post
  #1 (permalink)  
Old 04-18-2008, 02:12 PM
BHCluster BHCluster is offline
Member
 
Join Date: Mar 2008
Posts: 32
BHCluster is on a distinguished road
Problem in file implementation
Hi all
I need a bit of help here I have wore a program do take in a declared array as shown bellow but my problem is how to convert this program to read the numbers which I have in the array in the main from a file so it calculate them automatically and outputs them back into new file.

Code:
import java.io.*; import static java.lang.Math.*; class numbers { 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) { double[] gene={-9.30, 5.65, -5.02}; double[] gene1={-4.10, 5.68, 0.56}; 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); } }
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 -0.71 -2.64 -5.64
If somebody know the way to do this please help.
Thank you.
Reply With Quote
Sponsored Links