|
|
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.
|
|

04-19-2008, 10:20 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 22
|
|
|
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:
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:
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:
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:
-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!
|
|

04-19-2008, 11:39 PM
|
|
Moderator
|
|
Join Date: Nov 2007
Posts: 1,415
|
|
|
It looks like you are tying to convert a String array to double. This is the wrong approach. You should convert each array element to double value and if necessary store it in a double array afterwards. Write a loop and convert each string value one by one in this loop.
__________________
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.
|
|

04-19-2008, 11:46 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 22
|
|
|
Thanks for taking interest but can you show me how to do that cos I have lost my brain trying to crack it.
|
|

04-20-2008, 08:03 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 508
|
|
|
Yes, You can do that....
Try to convert the String elements in that String array to Double....
Not Array of String to Double.... Do you know about Java Core API docs?
regards,
sukatoa
|
|

04-20-2008, 09:19 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 22
|
|
|
Dude if new that I would not be bothering you. Please show me how to do this. Please.
|
|

04-21-2008, 02:23 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 508
|
|
String data[] = {"1.1","2.2","3.3","4.4"};
Double converted[] = new Double[5];
int x=0;
while(x<5){
converted[x] = Double.parseDouble(data[x]);x++;
}
Try to have an experiment on it....
regards,
sukatoa
|
|

04-21-2008, 06:57 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Originally Posted by sukatoa
String data[] = {"1.1","2.2","3.3","4.4"};
Double converted[] = new Double[5];
int x=0;
while(x<5){
converted[x] = Double.parseDouble(data[x]);x++;
}
Try to have an experiment on it....
regards,
sukatoa
Nice work pal, and good practice
String[] data = {"1.1","2.2","3.3","4.4"};
__________________
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.
|
|

04-21-2008, 11:41 AM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 237
|
|
Dude if new that I would not be bothering you. Please show me how to do this. Please.
To be serious in Java you really need to start looking at the API.
Java 2 Platform SE 5.0
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. || Don't forget to: 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.
|
|

04-21-2008, 07:43 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
|
|
Originally Posted by DonCash
To be serious in Java you really need to start looking at the API.
Right on!
BH- check out the Java 6 API too.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

04-22-2008, 04:34 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Captain, Don, sukatoa,
Does anyone of you know an application where we can find all Java doc, APIs and so on. A link...! 
__________________
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.
|
|

04-22-2008, 05:18 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
|
|
Originally Posted by Eranga
Captain, Don, sukatoa,
Does anyone of you know an application where we can find all Java doc, APIs and so on. A link...! 
Eranga, I'm afraid I don't understand what you mean by application that finds the API's.... if you're looking for the many different APIs- they're all located on Sun's site...
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

04-22-2008, 05:29 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Ah, application mean a program like MSDN. I seen such a tool one of my friend use. But he don't give any detail about it. 
What I try to do is make a collection of APIs and Docs myself.
__________________
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.
|
|

04-22-2008, 11:20 AM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 237
|
|
Are you looking to write your own APIs & Docs? Have you read about the JavaDoc tool?
javadoc-The Java API Documentation Generator
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. || Don't forget to: 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.
|
|

04-22-2008, 11:35 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
No Don, I'm not try to write my own stuff. I just want to make a collection. Say I'm working without a network connection, if I have a such collection no need to worried about 
__________________
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.
|
|

04-22-2008, 11:39 AM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 237
|
|
|
Oh right... You want to download the entire API documents to your computer.
I think they are available for download but i'm not sure where they are. I cant seem to find them.
You could try software that downloads the entire contents of a webpage. Check google..
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. || Don't forget to: 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.
Last edited by DonCash : 04-22-2008 at 11:47 AM.
|
|

04-22-2008, 11:51 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
|
|
Yes few softwares available which can we used. But the reason is, not possible to find all of them in a single place.
__________________
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|