altering code to read a string, distance calculations

Hi,

i have code which calculates the distance between all possible sets of atom interactions, using x,y and z co-ordinates. This information is defined in the code. Howeve, i am having problems in expanding this so that this information is read directly from an input file
(same format), which can contain 100 atoms.

import java.util.StringTokenizer;

public class AtomDistanceTest {
public static void main(String[] args) {
new AtomDistanceTest();
}

public AtomDistanceTest() {
try {
Atom a = new Atom("ATOM 5 CA PHE 1 113.142 75.993 130.862", "A");
Atom b = new Atom("ATOM 25 CA ARG 2 113.940 72.288 131.419", "B");
Atom c = new Atom("ATOM 49 CA TYR 3 116.236 72.885 134.471", "C");

Atom.printDistance(a, b);
Atom.printDistance(a, c);
Atom.printDistance(b, c);
} catch (Exception ex) {
System.out.println (ex);
}
}

}

class Atom {
public double x;
public double y;
public double z;
public String name;

public Atom(String s, String name) throws IllegalArgumentException {
try {
this.name = name;
StringTokenizer t = new StringTokenizer (s, " ");
for (int j=0; j<5; j++) t.nextToken(); //skip first 5 tokens
this.x = new Double(t.nextToken()).doubleValue();
this.y = new Double(t.nextToken()).doubleValue();
this.z = new Double(t.nextToken()).doubleValue();
} catch (Exception ex) {
throw new IllegalArgumentException ("Illegal atom description");
}
}

public String toString() {
return "atom : " + name + "(x=" + x + " y=" + y + " z=" + z + ")";
}

public double distanceFrom (Atom other) {
return calculateDistance (x, y, z, other.x, other.y, other.z);
}

public static double calculateDistance (double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt(Math.sqrt(Math.pow(Math.abs(x1-x2),2)+Math.pow(Math.abs(y1-y2),2))+Math.pow(Math.abs(z1-z2),2));
}

public static void printDistance (Atom a, Atom b) {
System.out.println ("Distance between " + a.toString() + " and " + b.toString() + " is " + a.distanceFrom(b));
}
}

indie_campbellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
 Use the following to open a file:

BufferedReader in = new BufferedReader(new FileReader("<the fiel you want to read>"));

  and the following to read the file line by line and create the Atom:

String line;
while ((line = in.readLine()) != null)
{
   Atom a = new Atom(line, <you read this data form file as well>);
}

  The rest should be more or less the same.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
indie_campbellAuthor Commented:
i will give it a go
0
indie_campbellAuthor Commented:
i am little confused by the statement u have written Atom a....etc
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

indie_campbellAuthor Commented:
regarding that in the original code Atom a , which had defined co-ordinate information
0
Ajay-SinghCommented:
Try this:-

public AtomDistanceTest() {
   try {
      LineNumberReader reader = new LineNumberReader(new FileReader("config.prop"));
      //read one line from this file.
      String line = reader.readLine();
      while(line!=null) {
          //create an atom object.
          Atom a = new Atom(line);
          line = readLine();
      }
   }catch(Exception ex) {
     System.out.println(ex);
   }
}
0
indie_campbellAuthor Commented:
i have tried this read in procedure, see below....having problems with the Atom a =new Atom(line)

import java.util.StringTokenizer;
import java.io.*;
import java.lang.*;

public class AtomDistanceTest1 {
     public static void main(String[] args) {
          new AtomDistanceTest1();
     }

     public AtomDistanceTest1() {
          try {
              LineNumberReader reader = new LineNumberReader(new FileReader("model2test.pdb"));
                    String line = reader.readLine();
                    while(line !=null) {
               Atom a = new Atom(line);
                        line = readLine();
          }} catch (Exception ex) {
               System.out.println (ex);
          }
     }

     }

class Atom {
     public double x;
     public double y;
     public double z;
     public String name;

     public Atom(String s, String name) throws IllegalArgumentException {
          try {
               this.name = name;
               StringTokenizer t = new StringTokenizer (s, " ");
               for (int j=0; j<5; j++) t.nextToken(); //skip first 5 tokens
               this.x = new Double(t.nextToken()).doubleValue();
               this.y = new Double(t.nextToken()).doubleValue();
               this.z = new Double(t.nextToken()).doubleValue();
          } catch (Exception ex) {
               throw new IllegalArgumentException ("Illegal atom description");
          }
     }

     public String toString() {
          return "atom : " + name + "(x=" + x + " y=" + y + " z=" + z + ")";
     }

     public double distanceFrom (Atom other) {
          return calculateDistance (x, y, z, other.x, other.y, other.z);
     }

     public static double calculateDistance (double x1, double y1, double z1, double x2, double y2, double z2) {
          return Math.sqrt(Math.sqrt(Math.pow(Math.abs(x1-x2),2)+Math.pow(Math.abs(y1-y2),2))+Math.pow(Math.abs(z1-z2),2));
     }

     public static void printDistance (Atom a, Atom b) {
          System.out.println ("Distance between " + a.toString() + " and " + b.toString() + " is " + a.distanceFrom(b));
     }
}

0
CEHJCommented:
You don't say what difficulty though!

1. There's no need to use the specialised LineNumberReader - you're not using its specialised functionality. Use BufferedReader instead.

2. You must close the reader when you've finished with it.

3. Create a separate AtomParser class that returns an Atom instance - it should not be the reponsibility of a class' constructor to parse its own arguments.

String line = null;
while ((line = in.readLine) != null) {
  Atom a = AtomParser.getAtom(line);
}
// Now close the reader
0
Ajay-SinghCommented:
Since the constructor of Atom class takes two arguments, It should be :-

public AtomDistanceTest1() {
         try {
             LineNumberReader reader = new LineNumberReader(new FileReader("model2test.pdb"));
                   String line = reader.readLine();
                   int count=0;
                   while(line !=null) {
              Atom a = new Atom(line, String.valueOf(count));
                       line = readLine();
         }} catch (Exception ex) {
              System.out.println (ex);
         }
    }
0
indie_campbellAuthor Commented:
yes, i totally agree with u on the Reader situation

but i dont understand the use of parsing here, could u expand??
0
CEHJCommented:
Are you talking to me? (no i'm not standing looking in a mirror wearing a shoulder holster ;-))
0
indie_campbellAuthor Commented:
this is what i have, put comments where problem

thanks

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

public class AtomDistanceTest1edit {
     public static void main(String[] args) {
          new AtomDistanceTest1edit();
                   
     }

     public AtomDistanceTest1edit() {
          try {
              BufferedReader reader = new BufferedReader(new FileReader("model2test.pdb"));
              String line = null;
              while((line = reader.readLine()) !=null) {
               Atom a = AtomParser.getAtom(line);
                                                      }
             
          reader.close();
              }

                  catch (Exception ex) {
               System.out.println (ex);
          }
               }
}
     

class AtomParser {

System.out.println("Parsing...");

          //loop to go through all lines of the file
          for (int i = 0; i < AtomParser.size(); i++) {
               ??? = AtomParser.elementAt(i).toString();    // this is the problem area???


}




class Atom {
     public double x;
     public double y;
     public double z;
     public String name;

     public Atom(String s, String name) throws IllegalArgumentException {
     
     

     try {
               this.name = name;
               StringTokenizer t = new StringTokenizer (s, " ");
               for (int j=0; j<5; j++) t.nextToken(); //skip first 5 tokens
               this.x = new Double(t.nextToken()).doubleValue();
               this.y = new Double(t.nextToken()).doubleValue();
               this.z = new Double(t.nextToken()).doubleValue();
          } catch (Exception ex) {
               throw new IllegalArgumentException ("Illegal atom description");
          }
     }

     public String toString() {
   

          return "atom : " + name + "(x=" + x + " y=" + y + " z=" + z + ")";
     }

     public double distanceFrom (Atom other) {
          return calculateDistance (x, y, z, other.x, other.y, other.z);
     }

     public static double calculateDistance (double x1, double y1, double z1, double x2, double y2, double z2) {
          return Math.sqrt(Math.sqrt(Math.pow(Math.abs(x1-x2),2)+Math.pow(Math.abs(y1-y2),2))+Math.pow(Math.abs(z1-z2),2));
     }

     public static void printDistance (Atom a, Atom b) {
          System.out.println ("Distance between " + a.toString() + " and " + b.toString() + " is " + a.distanceFrom(b));
     }
}

0
tomArghhCommented:
Be careful, you're double-posting. Click on 'Reload This Question' top left.
0
tomArghhCommented:
You need to put the 'getAtom' stuff into the AtomParser class. Make it a static method.
0
CleanupPingCommented:
indie_campbell:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
tomboshellCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept girionis's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.