|
|
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-18-2008, 01:12 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 32
|
|
|
Problem in file implementation
Hi all
I need a bit of help here I have wore a program do take in a declared array as shown bellow but my problem is how to convert this program to read the numbers which I have in the array in the main from a file so it calculate them automatically and outputs them back into new file.
import java.io.*;
import static java.lang.Math.*;
class numbers
{
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)
{
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);
double PearsonDist = Pearson(gene, gene1);
System.out.println("Pearson = " + PearsonDist);
}
}
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
-0.71 -2.64 -5.64
If somebody know the way to do this please help.
Thank you.
|
|

04-18-2008, 01:16 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
|
Read the file line by line and get substring from there. It's easy because there is a space between two number of each line. You got it?
__________________
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. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-18-2008, 01:33 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
|
Is there two tab spaces between two values in a single line?
__________________
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. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-18-2008, 02:04 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 32
|
|
|
Thanks for taking interest, and to answer your question the space between columns is not important, what is important is that there are three columns.
|
|

04-18-2008, 02:56 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 32
|
|
I have added this so it reads the files one by one:
Get Set methods
package get.set; // packaged for reuse
public class experiment
{
private double experiment1;
private double experiment2;
private double experiment3;
// no-argument constructor calls other constructor with default values
public experiment()
{
this ( 0.0, 0.0, 0.0 ); // call four-argument constructor
} // end no-argument AccountRecord constructor
// initialize a record
public experiment( double exp1, double exp2, double exp3 )
{
setExperiment1( exp1 );
setExperiment2( exp2 );
setExperiment3( exp3 );
} // end three-argument experiment constructor
// set experiment1
public void setExperiment1( double exp1 )
{
experiment1 = exp1;
} // end method setExperiment1
// get account number
public double getExperiment1()
{
return experiment1;
} // end method getExperiment1
// set experiment2
public void setExperiment2( double exp2 )
{
experiment2 = exp2;
} // end method setExperiment2
// get first name
public double getExperiment2()
{
return experiment2;
} // end method getExperiment2
// set set experiment3
public void setExperiment3( double exp3 )
{
experiment3 = exp3;
} // end method setExperiment3
// get experiment3
public double getExperiment3()
{
return experiment3;
} // end method getExperiment3
} // end class experiment
Read file class
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import get.set.experiment;
public class readFile
{
private Scanner input;
// enable user to open file
public void openFile()
{
try
{
input = new Scanner( new File("Data_Set_1.txt") );
} // end try
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
System.exit( 1 );
} // end catch
} // end method openFile
// read record from file
public void readRecords()
{
// object to be written to screen
experiment record = new experiment();
System.out.printf( "%10s%15s%15s\n", "Experiment 1",
" Experiment 2", " Experiment 3" );
try // read records from file using Scanner object
{
while ( input.hasNext() )
{
record.setExperiment1( input.nextDouble() ); // read experiment1
record.setExperiment2( input.nextDouble() ); // read experiment2
record.setExperiment3( input.nextDouble() ); // read experiment3
// display record contents
System.out.printf( "%10f%15f%15f\n",
record.getExperiment1(), record.getExperiment2(),record.getExperiment3());
} // end while
} // end try
catch ( NoSuchElementException elementException )
{
System.err.println( "File improperly formed." );
input.close();
System.exit( 1 );
} // end catch
catch ( IllegalStateException stateException )
{
System.err.println( "Error reading from file." );
System.exit( 1 );
} // end catch
} // end method readRecords
// close file and terminate application
public void closeFile()
{
if ( input != null )
input.close(); // close file
} // end method closeFile
} // end class readFile
Test Class
public class runReadFileTest
{
public static void main( String args[] )
{
readFile application = new readFile();
application.openFile();
application.readRecords();
application.closeFile();
} // end main
} // end class runReadFileTest
Ok my next question is how to use algorithms from above to combine with these classes so they do waht they are suppose to do. Your help is greatly appreciated
Last edited by BHCluster : 04-18-2008 at 04:55 PM.
|
|

04-21-2008, 04:09 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
Originally Posted by BHCluster
Thanks for taking interest, and to answer your question the space between columns is not important, what is important is that there are three columns.
Have a reason to ask that question. After reading a line of a file, easily use the StringTokenizer class to get those three columns. So simple it is 
__________________
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. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-21-2008, 04:21 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
|
|
I'm not sure how and why you going to use algorithms for this. In simple way you can read the file and get the numbers of each columns. As i said earlier, space in between two numbers of a each line is important for my application. I assumed that the space is two tabs.
FileInputStream fis = null;
try {
fis = new FileInputStream("files/ReadFileByLines.txt");
InputStreamReader isr = new InputStreamReader(fis);
LineNumberReader lnr = new LineNumberReader(isr);
String line = null;
while((line = lnr.readLine()) != null){
StringTokenizer st1 = new StringTokenizer(line, "\t\t");
String s1 = st1.nextToken();
String s2 = st1.nextToken();
String s3 = st1.nextToken();
System.out.println(s1 + "\t" + s2 + "\t" + s3);
}
System.out.println("Total number of lines on the file " +
"is " + lnr.getLineNumber());
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
finally {
try {
fis.close();
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
__________________
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. (Close on September 4, 2008)
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
|
|
|
|
|