Results 1 to 5 of 5
  1. #1
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default Question about loops

    I have a class like this
    Java Code:
    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;
    }
    In that class there is going to be 60 Y and 3 X and from which Result for all is going to be X-Y so in stead of writing the above lines 60 time i was wondering how this can be done ith a loop if it can be done.

    Example:
    Java Code:
              x1                x2                x3
    y1    -9.30		5.65		-5.02
    y2    -4.10		5.68		0.56
    y3    4.75		0.11		2.86
    y4    -1.54		-10.01		-1.99
    y5    -6.29		5.62		-2.78
    y6    -13.02		1.31		1.61
    y7    -5.85		3.79		-6.56
    y60  -12.71		-0.45		0.62
    Any help is greatly appreciated.
    Last edited by BHCluster; 04-16-2008 at 05:23 PM.

  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    You could put it into a for loop..

    Java Code:
    for(int i = 0; i < 60; i++){
    		    	
    }
    Did this post help you? Please me! :cool:

  3. #3
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    you mean like this
    public static double Euclidean(double[] array1, double[] array2){

    //Store the numbers of the both arrays
    double x;
    double y

    String[] arrayX;
    arrayX = new String[3];

    // Read each line into the array
    for (int i = 0; i < arrayX.length; i++){
    arrayX[i] = x();
    }

    String[] arrayY;
    arrayY = new String[60];

    // Read each line into the array
    for (int i = 0; i < myarrayY.length; i++){
    arrayY[i] = y();
    }

    }

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Yeah looks good to me. What happens when you compile it?
    Did this post help you? Please me! :cool:

  5. #5
    BHCluster is offline Member
    Join Date
    Mar 2008
    Posts
    32
    Rep Power
    0

    Default

    I know you get pm alot but I am bit desperate because I don't know how to convert this:
    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 = 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 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);
    
    	}
    
    }
    So it reads the file with 3 across and 60 down numbers. I have this code bellow which reads file and displays it on the screen but I cannot seem to get it to do what the code above does. If you know how to do it please help cos I don't know.

    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;
    import static java.lang.Math.*;
    
    public class csv {
    public static double Euclidean(double[] array1, double[] array2){
    
    //Store the numbers of the both arrays
    	double x;
    	double y
    
        String[] arrayX;
    	arrayX = new String[3];
    
    	// Read each line into the array
    	for (int i = 0; i < arrayX.length; i++){
    		arrayX[i] = x();
    	}
    
    	String[] arrayY;
    	arrayY = new String[60];
    
    	// Read each line into the array
    	for (int i = 0; i < myarrayY.length; i++){
    		arrayY[i] = y();
    	}
    
    	String[] MinusResults;
    	MinusResults = new String[61];
    
    	// Read each line into the array
    	for (int i = 0; i < MinusResults.length; i++){
    		MinusResults[i] = y() - x();
    	}
    
    
    
    }
    
    
    public static double Manhattan(double[] array1, double[] array2){
    
    }
    
    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("\"", ""));
    
    	}
    
      }
    }

    Any help is greatly appreciated. Thank you for taking interest.

Similar Threads

  1. [SOLVED] Need help with Slope (Loops)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 04-18-2008, 04:39 AM
  2. [SOLVED] Need help with Loops...please!
    By Zebra in forum New To Java
    Replies: 5
    Last Post: 04-10-2008, 02:44 PM
  3. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 04:59 AM
  4. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 05:51 PM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •