Results 1 to 4 of 4
 04162008, 11:44 AM #1Member
 Join Date
 Mar 2008
 Posts
 32
 Rep Power
 0
Some problems with arrays and loops
Hi all
My problem is that i cannot covert simple program which I wrote to the one that would reduce the size of it and do the same. Main problem are the arrays and loops. What I am trying to do is read in the file as shown below and run it trough the algorithms which are in the "numbers class" and out put the results on screen and in the file. I have made some progress with reading the file in and string it into array (code was from the forum) Yes I am re using code.
I did this and it works I know it is simple:
Java Code:import java.io.*; import static java.lang.Math.*; class numbers 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 = y1x1; double minusResult2 = y2x2; double minusResult3 = y3x3; //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 = y1x1; double minusResult2 = y2x2; double minusResult3 = y3x3; //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 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); } }
Java 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; public class csv { public static double Euclidean(double[] array1, double[] array2){ // Declare Array String[] myarray1; String[] myarray2; String[] ResultArray; arrayX = new String[3]; arrayY = new String[61]; ResultArray = new String[61]; double array1 = new double [arrayX]; double array2 = new double [arrayY]; double array3 = new double [ResultArray]; array3 = array2  array1; // Read each line into the array for (int i = 0; i < ResultArray.length; i++){ double square = pow(array3,2); } double sumOfSquares = sum(square); //Get the square root of the sumOfSquares double finalResult = sqrt(sumOfSquares); 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]; // 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("\"", "")); } } }
Java Code:Experiment 1 Experiment 2 Experiment 3 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 2.77 4.16 3.83 0.25 0.58 0.77 2.52 5.80 8.80 5.53 9.11 1.92 6.63 2.53 7.94 7.11 8.09 6.47 0.30 6.18 6.25 0.33 4.58 8.32 2.39 1.44 3.94 7.70 9.39 4.73 4.89 6.17 6.98 6.05 6.54 1.94 6.78 7.83 7.72 4.86 2.31 2.33 6.93 3.95 10.61 0.04 7.31 1.49 4.78 3.41 10.69 5.55 6.48 0.56 3.18 4.32 3.32 3.67 9.79 13.03 11.01 3.69 5.13 2.24 2.19 12.74 8.84 2.10 1.28 0.69 7.11 5.35 9.19 6.10 11.01 2.37 6.58 2.67 6.40 6.03 3.58 0.54 1.32 0.69 0.45 0.01 0.25 0.52 0.96 0.28 0.86 1.07 0.96 0.47 1.36 0.81 0.64 1.47 0.32 0.34 0.47 0.46 0.27 0.62 0.43 0.49 0.22 0.01 9.80 2.34 5.95 6.74 8.43 4.21 7.60 6.57 5.13 6.53 2.55 13.86 3.82 7.12 11.45 5.49 7.60 0.40 6.95 2.61 5.14 0.21 6.98 0.84 1.65 3.77 3.33 6.16 3.25 14.31
Thank you.
 04162008, 11:48 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 04162008, 12:09 PM #3Member
 Join Date
 Mar 2008
 Posts
 32
 Rep Power
 0
ok
what i am trying to figure out is how to do this
class numbers
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 = y1x1;
double minusResult2 = y2x2;
double minusResult3 = y3x3;
//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 Euclidean(double[] array1, double[] array2){
// Declare Array
String[] myarray1;
String[] myarray2;
String[] ResultArray;
arrayX = new String[3];
arrayY = new String[61];
ResultArray = new String[61];
double array1 = new double [arrayX];
double array2 = new double [arrayY];
double array3 = new double [ResultArray];
array3 = array2  array1;
// Read each line into the array
for (int i = 0; i < ResultArray.length; i++){
double square = pow(array3,2);
}
double sumOfSquares = sum(square);
//Get the square root of the sumOfSquares
double finalResult = sqrt(sumOfSquares);
return finalResult;
}
Thanks
 04162008, 12:24 PM #4
Similar Threads

[SOLVED] Need help with Slope (Loops)
By Zebra in forum New To JavaReplies: 9Last Post: 04182008, 03:39 AM 
[SOLVED] Need help with Loops...please!
By Zebra in forum New To JavaReplies: 5Last Post: 04102008, 01:44 PM 
[SOLVED] Problems with arrays
By hayjk in forum New To JavaReplies: 3Last Post: 04072008, 05:52 PM 
Loops (while do etc)
By manupr in forum New To JavaReplies: 1Last Post: 01152008, 04:59 AM 
Problems with arrays
By Marcus in forum New To JavaReplies: 2Last Post: 07042007, 08:10 AM
Bookmarks