The Artima Developer Community
Sponsored Link

Java Answers Forum
Reading values from an array

5 replies on 1 page. Most recent reply: Jul 16, 2003 12:46 PM by Mr Plow

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Chris Brooks

Posts: 6
Nickname: bioheel
Registered: Jul, 2003

Reading values from an array Posted: Jul 9, 2003 9:36 AM
Reply to this message Reply
Advertisement
This is likely a simple task, but as I have been trying to teach myself java from online the API list & Sun tutorials I am struggling to figure this one out.

The program (at least is supposed to) reads values into an array which is then parsed into three separate vectors representing an x-value, y-value and an id. I am trying to then read values from the arrays in a way that keeps one stable while moving through all the rest and doing a simple calculation.

CODE___________________________________
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

public class EdgeDist {
public static void main(String[] args) {

String[] locations = new String[244];
File f = new File ("sileneOut.txt");
FileReader fin;
BufferedReader locs;
double d1;
double d2;
int i3;
double[] xValue = new double[244];
double[] yValue = new double[244];
int[] idValue = new int[244];

//create buffers for reader and string and read data
try {
for (int k=0; k<245; k++){
fin = new FileReader(f);
locs = new BufferedReader(fin);
locations[k]=locs.readLine();
}
}

catch (Exception e){
System.exit(0);
}

for (int k=0; k<245; k++){
StringTokenizer st = new StringTokenizer(locations[k], ",");

String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();

d1 = Double.parseDouble (s1);
d2 = Double.parseDouble (s2);
i3 = Integer.parseInt (s3);

xValue [244] = d1;
yValue [244] = d2;
idValue [244] = i3;
}

System.out.println("\n Number of Arguments: " + idValue.length);
//EVERYTHING'S GOOD TO RIGHT HERE*********************

double XpointA;
double YpointA;
int IDpointA;
double XpointB;
double YpointB;
int IDpointB;
BufferedWriter clusters;
FileWriter fir;

//loop for threshold distance
for (double d=0.2; d<2.2; d+=0.2){
File a = new File("distances.txt");

//loop for first point
for (int e=0; e<244; e++){
//read first point
XpointA = getDouble(xValue[244], e);
YpointA = getDouble(yValue[244], e);
IDpointA = getInt(idValue[244], e);

//loop for second point
for (int g=1; g<245; g++){
//read second point
XpointB = getDouble(xValue[244], g);
YpointB = getDouble(yValue[244], g);
IDpointB = getInt(idValue[244], g);

//calculate Euclidean distance between points
double diffX = (XpointA - XpointB);
double diffY = (YpointA - YpointB);
double diffX2 = (diffX * diffX);
double diffY2 = (diffY * diffY);
double sumdiff2 = (diffX2 + diffY2);
double dist = sqrt(sumdiff2);

//write data to comma-delimited text file
fir = new FileWriter(a, true);
clusters = new BufferedWriter(fir);
a.write("\n"+IDpointA+","+IDpointB+","+dist);
}
}
}
}
}
_______________________________________________________
What I get is a "cannot resolve symbol" message on every getDouble, getInt & sqrt in the file. Is there something obvious that I am missing?


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Reading values from an array Posted: Jul 13, 2003 9:57 AM
Reply to this message Reply
I am not sure what you are expecting the getDouble() and getInt() function to do. If you explain in little more detail I migght be able to help you...

However the sqrt function instead of

double dist = sqrt(sumdiff2);

have

double dist = Math.sqrt(sumdiff2);

this should work...!!!

Chris Brooks

Posts: 6
Nickname: bioheel
Registered: Jul, 2003

Re: Reading values from an array Posted: Jul 14, 2003 11:01 AM
Reply to this message Reply
I appreciate the reply but I have moved on to another problem that I cannot seem to solve. I am trying to read x,y coordinates with an ID value from a text file and calculate pairwise distances for every pair of points. Then I want to write the two ID values along with the distances to another text file. I can get the values to print on the screen with the System.out.println command but I can't seem to get it to write to a file -- it creates an empty file only. What is this novice doing wrong?

import java.io.*;
import java.util.*;

//Creates a file reader that reads in x,y coordinates from a text file.
//arguments[0] specifies filename and path to read.
//arguments[1] specifies the length of the multi-dimensional array.
//arguments[2] specifies the name of the output file.


public class DMatrix {
public static void main(String[] arguments) {

String alength = arguments[1];
int length = Integer.parseInt(alength);
String[] location = new String [length];
File inFileName = new File (arguments[0]);
FileReader fin;
BufferedReader locs;
String name = new String (arguments[2]);
File outFileName = new File(name);

try {
fin = new FileReader(inFileName);
locs = new BufferedReader(fin);
for (int k=0; k<length; k++){
location[k] = locs.readLine();
}
}
catch (Exception e){
System.out.println("Program Error - Aborting");
}

for (int k=0; k<length; k++){
double[] axValue = new double[length];
double[] ayValue = new double[length];
int[] aidValue = new int[length];

StringTokenizer st = new StringTokenizer(location[k], ",");

String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();

double d1 = Double.parseDouble (s1);
double d2 = Double.parseDouble (s2);
int i3 = Integer.parseInt (s3);

axValue[k] = d1;
ayValue[k] = d2;
aidValue[k] = i3;

for (int m=1; m<(length-1); m++){
double[] bxValue = new double[length];
double[] byValue = new double[length];
int[] bidValue = new int[length];

StringTokenizer stb = new StringTokenizer(location[m], ",");

String s4 = stb.nextToken();
String s5 = stb.nextToken();
String s6 = stb.nextToken();

double d4 = Double.parseDouble (s4);
double d5 = Double.parseDouble (s5);
int i6 = Integer.parseInt (s6);

bxValue[m] = d4;
byValue[m] = d5;
bidValue[m] = i6;

double diffx = (bxValue[m] - axValue[k]);
double diffy = (byValue[m] - ayValue[k]);
double sqrx = (diffx * diffx);
double sqry = (diffy * diffy);
double sumsq = (sqrx + sqry);
double distance = Math.sqrt (sumsq);

try {
FileWriter w = new FileWriter(outFileName, true);
String newData = new String("s3"+","+"s6"+","+"distance");
w.write(newData+"\r");
}
catch (IOException ioe) {
System.out.println("Program Error - Aborting");
}
}
}
}
}

Mr Plow

Posts: 18
Nickname: mrplow
Registered: Jun, 2003

Re: Reading values from an array Posted: Jul 14, 2003 1:57 PM
Reply to this message Reply
I took a look at your try block code where you were trying to write output to a file.

Here are a couple of commments and some working code which I hope are helpful:

1) I believe that the statement:
String newData = new String("s3"+","+"s6"+","+"distance");
is not doing what you intend. If you surround an reference like s3 with double quoatation marks, the java compiler will interpet the identifier to be a String object rather than a reference to an object.

That is to say that if:
String s3 = "1", s6 = "2", distance = "3";
The way that you have written the statement, the value of String newData would be "s3,s6,distance". To get the desired value of "1,2,3" you need to write the statement like:
String newData = s3 + "," + s6 + ", " + distance;


2) To use the Java I/O classes correctly they must piggy-back on each others functionality. To demonstrate how this works for outputing text to a file, I have created a small "Tester" class below in which I simplified the code in your try/catch block.


import java.io.*;

public class Tester
{
public static void main(String[] args)
{
String s3 = "1", s6 = "2", distance = "3";
String outFileName = "out.txt";

try {
String output = s3 + ", " + s6 + ", " + distance;
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outFileName)));
out.println(output);
out.close();
} catch (IOException e) {
System.err.println("IOException thrown:" + e );
}
}
};

Chris Brooks

Posts: 6
Nickname: bioheel
Registered: Jul, 2003

Re: Reading values from an array Posted: Jul 15, 2003 9:37 AM
Reply to this message Reply
Thanks for your help - now the program writes to a file BUT it overwrites the data at each step so that in the end the file has only 1 line of data.

I was afraid of this before but I don't know how to keep the value of distance, etc. outside of the for loop so that I can write to the file without initializing a new writer at each step. Can you help?

Mr Plow

Posts: 18
Nickname: mrplow
Registered: Jun, 2003

Re: Reading values from an array Posted: Jul 16, 2003 12:46 PM
Reply to this message Reply
Here is a revised sample Tester class. Note that the key is to first collect all your data in one String object (in the example below it is in the String object that the reference "all" points to) before attempting to write any of it to a file. Once you have the String object containing your ouput, you need to put this into a BufferedReader that you can then use to pull out each line individually.


import java.io.*;

public class Tester
{
public static void main(String[] args)
{
String one = "Line 1\n", two = "Line 2\n", three = "Line 3\n", four = "Line 4\n", five = "Line 5\n";
String all = one + two + three + four + five;
String a;


String outFileName = "out.txt";

try {
BufferedReader in = new BufferedReader(new StringReader(all));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outFileName)));
while( ( a = in.readLine() ) != null)
{
out.print(a + "\n");
}
out.close();
} catch (IOException e) {
System.err.println("IOException thrown:" + e );
}
}
};

Flat View: This topic has 5 replies on 1 page
Topic: interview questions Previous Topic   Next Topic Topic: how to put three button into one borders

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use