We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › access Class correctly?
Page Index Toggle Pages: 1
access Class correctly?? (Read 574 times)
access Class correctly??
Jan 18th, 2006, 4:48pm
 
Hi, this obviously loads a matrix, 7x7 in this case. Perfect. that’s what i was looking for..but how can i correctly implement and access it for example, i mean whats the code in setup/draw to access the class and get out the data from arrays?

The class should be ok, so i guess i might do something wrong in trying to access it.
Sorry for this and thanks very much for helping me out…



class Loadata
{

 String name;  
 int[][] data;

 Loadata(String _name)
 {
   name=_name;
   loadString();
 }

 void loadString()
 {

   data = new int[7][7];
   String[]t=loadStrings(name+".txt");
   for(int i=0; i<t.length; i++)
   {
String[]rawData=split(t[i], ",");
for(int j=0; j<rawData.length; j++)
{
  data[i][j] = Integer.parseInt(rawData[j]);
}
   }
 }

 int[] getData(int where)
 {
   return data[where];  
 }

}
Re: access Class correctly??
Reply #1 - Jan 21st, 2006, 12:05am
 
Create a new sketch. Create a folder called 'data' in that sketch's directory. Copy the following and paste it into a file called 'text.txt' and save it in the new 'data' folder.

Code:

1,2,3,4,5,6,7
8,9,0,1,2,3,4
5,6,7,8,9,0,1
2,3,4,5,6,7,8
9,0,2,3,4,5,6
7,8,9,0,1,2,3
4,5,6,7,8,9,0


Put the following code into your new sketch:

Code:

LoadData test;
void setup(){
test = new LoadData("text.txt");
noLoop();
}
void draw(){
for(int i = 0; i < test.data.length; i++){
for(int j = 0; j < test.data[i].length; j++){
print(test.data[i][j] + " ");
}
println();
}
}
class LoadData{
String fileName;
int [][] data;
LoadData(String fileName){
String [] temp = loadStrings(fileName);
data = new int [7][7];
for(int i = 0; i < data.length; i++){
String [] tempLine = split(temp[i], ',');
for(int j = 0; j < data[i].length; j++){
data[i][j] = int(tempLine[j]);
}
}
}
}

You should have a working model that demonstrates what you were trying to achieve now. I've compacted what you were doing a lot because it seemed you could bunch all of your stuff into the class's constructor. I also set it up to demonstrate a method of iterating through a matrix of unknown size.

If there's anything that isn't too clear just say. I'll be happy to elaborate on my method. Hope this helps.
Re: access Class correctly??
Reply #2 - Jan 23rd, 2006, 3:45pm
 
st33d wrote on Jan 21st, 2006, 12:05am:
Create a new sketch. Create a folder called 'data' in that sketch's directory. Copy the following and paste it into a file called 'text.txt' and save it in the new 'data' folder.


you can also just make the file, and drag & drop text.txt into the processing window and the status bar will say "1 file added."

or use sketch -> add file to sketch, which will create the data folder for you and copy the file to that location.
Page Index Toggle Pages: 1