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

    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:

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

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

    Java 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!

  2. #2
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    9

    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.

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

    Default

    Thanks for taking interest but can you show me how to do that cos I have lost my brain trying to crack it.

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    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

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

    Default

    Dude if new that I would not be bothering you. Please show me how to do this. Please.

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Java 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

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sukatoa View Post
    Java 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

    Java Code:
    String[] data = {"1.1","2.2","3.3","4.4"};

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

    Default

    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! :cool:

  9. #9
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote 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)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Captain, Don, sukatoa,

    Does anyone of you know an application where we can find all Java doc, APIs and so on. A link...! :)

  11. #11
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote 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)

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

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

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

    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! :cool:

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

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

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

    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..
    Last edited by DonCash; 04-22-2008 at 10:47 AM.
    Did this post help you? Please me! :cool:

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 03:37 AM
  2. java error message
    By baileyr in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 03:47 AM
  3. Help mi solve my error
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-11-2008, 05:26 AM
  4. Replies: 7
    Last Post: 08-12-2007, 03:47 PM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 AM

Posting Permissions

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