hi there,

my programme stores all the coordinates from a text file into an arrayList. And from the arrayList I want to read out 3 elements at a time. Each element being a pair of (x, y) values already in a arrayList, e.g.[...., 34.123 2.121,....].

Now I need to read every 3 elements out from the arrayList as a set of 3 together, in a String.

my code fragment:

Iterator<String> itr = arrayList.iterator();
               while(itr.hasNext())
               {
               	String element = itr.next(); // this is just 1, how do i make it 3?
               	System.out.print(element + " ");
               }
               System.out.println();

thank you very much!

You have to call next() three times if you want three values.

Why not store all three values into a single object as you read them from the file?

It would help if you had three places to put the values.

Better still would be if you put the values into the ArrayList as a block of three. If a unit of data consists of three values, make it an object:

public class DataObject
{
private Point p1;
private Point p2;
private Point p3;

public DataObject (Point p1, Point p2, Point p3)
{
thisl.p1=p1; //etc
}

public Point getP1()
{ return p1;}

//etc
}

Something like that, only with better names for "p1", "p2", and "p3"

I'm assuming that three point pairs consitute some sort of meaningful set - otherwise, why are you wanting to read them three at a time?

yeah. Good thinking Jon. I need 3 points to define a "algorithmic" reference frame. but it isnt real... Thanks, I'll try that out and get back to you guys as soon as I can.

uummm, Jon, do i need to do this the JDO way?

I don't imagine so. I was thinking you'd just make a class that represents whatever it is your three point pairs represent (an algorithmic reference frame? yeah, one of those). Since I didn't know what your numbers were representing, I called it by a generic name, a DataObject.

ah. i see. ok. got me for a moment. thanks.

Sorry to bother you, I'm not getting it to work and I really have tried placing them together but I am very sure I am not doing it correct.. Do you see why? I've referred to other programmes. Now the main issue is that variable symbol "rikr" cannot be be found but to me it has already been declared in the constructor of the "dataobject" class. Really need your advice. Thank you.
**lines 6-41, 81-89

package basisgenesis;

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

//***********************************************************************************
class Trio {

	//private fields
	private String p1;
	private String p2;
	private String p3;

// constructor methods
public Trio(String p1, String p2, String p3)
{
this.p1=p1; //origin
this.p2=p2; //basis
this.p3=p3; //plane
}

//returning the value of private fields
//accessor for pt. origin
public String getp1()
{
	return p1;
}
//accessor for pt. basis
public String getp2()
{
	return p2;
}
//accessor for plane
public String getp3()
{
	return p3;
}

}

//************************************************************************************


public class BasisGenesis {

public static void main(String[] args)                                    //main method

 {
	  File dir = new File("1a00.gz.txt");
     String path = dir.getAbsolutePath();
     int root = path.lastIndexOf("\\");
     String howPath = path.substring(0,root+1);
     File usePath = new File(howPath);
     String[] fileDir = usePath.list();
     if(fileDir == null)
     {
     	System.out.println("Directory Does Not Exist");
     }
      else
      {
     	for(int j=0; j<fileDir.length;j++)
     	{
     	   String fileName = fileDir[j];
     	try
      {

     	    BufferedReader in = new BufferedReader(new FileReader(fileDir[j]));  //line by line
            String str;
            ArrayList<String> arrayList = new ArrayList<String>();

      		 while ((str = in.readLine()) != null)//for every line, String read in
      		 {
              //compute as an element of the arraylist
              //for each model (arraylist)
               arrayList.add(str) ;
               System.out.println( fileName + arrayList);
               System.out.println("You have just seen "+fileName+" in an arrayList.");


               //for each (ordered, noncollinear) triple of a model do:
               Iterator<String> itr = arrayList.iterator();

               while(itr.hasNext())
               {
                Trio rikr = new Trio(" "," "," ");
               }
               System.out.println(rikr.getp1()+ " " + rikr.getp2 + " " + rikr.getp3);
      		 }//end of while
             System.out.println("Coordinates of each Protein in the database are now in an arraylist.");
              in.close();
            }//end of try
         catch (IOException e) {
       	System.out.println("Failed to process file. Please try again.");}
     }//for
   }//end of else
 }//main method
}//end of class
while ((str = in.readLine()) != null)//for every line, String read in
  {
    //compute as an element of the arraylist
    //for each model (arraylist)

    arrayList.add(str) ;
    System.out.println( fileName + arrayList);
    System.out.println("You have just seen "+fileName+" in an arrayList.");
    Iterator<String> itr = arrayList.iterator();

    while(itr.hasNext())
      {
        Trio rikr = new Trio(" "," "," ");
      }

    System.out.println(rikr.getp1()+ " " + rikr.getp2 + " " + rikr.getp3);
  }//end of while
System.out.println("Coordinates of each Protein in the database are now in an arraylist.");

in.close();
}//end of try

Read through the code literally, as if you were the compiler. Each time you go through the outer while loop, you make an iterator for your arraylist of Strings, and you do a small block of code once for each member of that array. That code creates a Trio object initialized to contain a single space for each of its three members. That Trio then goes out of scope and disappears (a local variable only lasts as long as the block that declared it). Then you try to print that out of scope object, and you find that it doesn't exist. It doesn't, because it only existed betwen those curly braces, and you get an error.

So there's a few problems here. Have a go at fixing them, I'll check in later to see how you're coming along.

Thanks eh. And it won't be so soon. It's midnight here. I'll come back when morning breaks.

Hi Jon, it can be compiled now. However, its only showing

100d.gz.txt.txt[0.0 0.0] in the output which i believe it's because of

a Trio object initialized to contain a single space for each of its three members.

I suppose that my next step is actually to create another arraylist that stores each of the Trio objects. Am i right?

while ((str = in.readLine()) != null)//for every line, String read in
      		 {
              //compute as an element of the arraylist
              //for each model (arraylist)
               arrayList.add(str) ;
               System.out.println( fileName + arrayList);
               System.out.println("You have just seen "+fileName+" in an arrayList.");

               //for each (ordered, noncollinear) triple of a model do:
               Iterator<String> itr = arrayList.iterator();

               while(itr.hasNext())
               {
               	if(arrayList.size() >= 3)
               	{
                Trio rikr = new Trio(" "," "," ");
                System.out.println(rikr.getp1()+ " " + rikr.getp2() + " " + rikr.getp3());
                trio.add(rikr);
                arrayList.clear();
               	}
               }
               if(arrayList.size()>0)
               	System.err.println("Missing coordinates");

      		 }//end of while
             System.out.println("Coordinates of each Protein in the database are now in an arraylist.");
              in.close();
            }//end of try
         catch (IOException e) {
       	System.out.println("Failed to process file. Please try again.");}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.