Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-16-2008, 01:44 PM
Member
 
Join Date: Mar 2008
Posts: 32
BHCluster is on a distinguished road
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:

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); } }
And I am trying to use loops and arrays so I don't have to code all of this manually but I am making no progress.

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("\"", "")); } } }
File to be read
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
I f some one can help me or give me some pointers I would appreciate it .
Thank you.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-16-2008, 01:48 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by BHCluster View Post

And I am trying to use loops and arrays so I don't have to code all of this manually but I am making no progress.
What mean 'progress'? On performance, like taken to complete the task....
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 02:09 PM
Member
 
Join Date: Mar 2008
Posts: 32
BHCluster is on a distinguished road
ok
what i am trying to figure out is how to do this
Quote:
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;
}
61 times with out doing one by one manually, and what I have ended up is:
Quote:
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;

}
So what I am trying to say is can some one please tell me if I am doing this right or wrong.
Thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-16-2008, 02:24 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
About the file "file to be read"... it would be nice if you cascade it down...

so that, you may implement it in easier way... " by using scanner"....

kind regards,
sukatoa
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help with Slope (Loops) Zebra New To Java 9 04-18-2008 05:39 AM
[SOLVED] Need help with Loops...please! Zebra New To Java 5 04-10-2008 03:44 PM
[SOLVED] Problems with arrays hayjk New To Java 3 04-07-2008 07:52 PM
Loops (while do etc) manupr New To Java 1 01-15-2008 05:59 AM
Problems with arrays Marcus New To Java 2 07-04-2007 10:10 AM


All times are GMT +3. The time now is 10:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org