Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-19-2008, 10:20 PM
Member
 
Join Date: Mar 2008
Posts: 32
Rep Power: 0
BHCluster is on a distinguished road
Default 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:

Code:
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:

Code:
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:
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 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:

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
Thank you!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 11:39 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default
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.
__________________
"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 11:46 PM
Member
 
Join Date: Mar 2008
Posts: 32
Rep Power: 0
BHCluster is on a distinguished road
Default
Thanks for taking interest but can you show me how to do that cos I have lost my brain trying to crack it.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-20-2008, 08:03 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Default
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-20-2008, 09:19 PM
Member
 
Join Date: Mar 2008
Posts: 32
Rep Power: 0
BHCluster is on a distinguished road
Default
Dude if new that I would not be bothering you. Please show me how to do this. Please.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-21-2008, 02:23 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 540
Rep Power: 3
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Default
Code:
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-21-2008, 06:57 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,445
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by sukatoa View Post
Code:
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

Code:
String[] data = {"1.1","2.2","3.3","4.4"};
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-21-2008, 11:41 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 4
DonCash will become famous soon enoughDonCash will become famous soon enough
Default
Quote:
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 me!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-21-2008, 07:43 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 838
Rep Power: 4
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Default
Originally Posted by DonCash View Post
To be serious in Java you really need to start looking at the API.
Right on!

BH- check out the Java 6 API too.
__________________
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-22-2008, 04:34 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,445
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-22-2008, 05:18 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 838
Rep Power: 4
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Default
Originally Posted by Eranga View Post
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...
__________________
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-22-2008, 05:29 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,445
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-22-2008, 11:20 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 4
DonCash will become famous soon enoughDonCash will become famous soon enough
Default
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 me!
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-22-2008, 11:35 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,445
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-22-2008, 11:39 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 4
DonCash will become famous soon enoughDonCash will become famous soon enough
Default
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 me!

Last edited by DonCash; 04-22-2008 at 11:47 AM.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-22-2008, 11:51 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,445
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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
How to solve "No compiler error"? iceman New To Java 5 04-22-2008 04:37 AM
java error message baileyr New To Java 2 01-23-2008 04:47 AM
Help mi solve my error Deon New To Java 3 01-11-2008 06:26 AM
Help with error message when running JAR via HTML file Simmy AWT / Swing 7 08-12-2007 04:47 PM
error message on jsp sandor Web Frameworks 1 04-11-2007 03:10 AM


All times are GMT +2. The time now is 09:49 PM.



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