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 & HelpPrograms › Reading in a database
Page Index Toggle Pages: 1
Reading in a database (Read 1561 times)
Reading in a database
Feb 21st, 2010, 4:55am
 
Hi,
I am using an example to read in a database from a file. In the example it is a huge database of zipcodes, latitudes and longitudes. I have a small database containing lecturer's names,department and x,y coordinates on the map. Once I have read in the database I have clear instructions on how to refine my program but I have no idea how to read in the databse using processing. I have tried to use the given example but there is too much unnecessary parsing ect. Could anyone please give me tips on how I could read in the simple database and output the x,y coordinates when the lecturers name is typed in. Here is the code i have been trying to use:



static final int DEPARTMENT = 0;
static final int X = 1;
static final int Y = 2;
static final int NAME = 3;

int totalCount;  // total number of lecturers
Lecturer[] lecturers;
int lecturerCount;


// min/max boundary of all points
float minX, maxX;
float minY, maxY;


public void setup() {
 readData();
}

void readData() {
 String[] lines = loadStrings("lecturer.csv");
 parseInfo(lines[0]); // read the header line
 
 lecturers = new Lecturer[totalCount];
 for ( int i = 1; i < lines.length; i++){
   lecturers[lecturerCount] = parseLecturer(lines[i]);
   lecturerCount ++;
 }
}
void parseInfo(String line) {
 String infoString = line.substring(2);  // remove the #
 String[] infoPieces = split(infoString, ',');
 totalCount = int(infoPieces[0]);  
 minX = float(infoPieces[1]);
 maxX = float(infoPieces[2]);
 minY = float(infoPieces[3]);
 maxY = float(infoPieces[4]);
}

Lecturer parseLecturer(String line) {
 String pieces[] = split(line, TAB);
 String department = pieces[DEPARTMENT];
 String name = pieces[NAME];
 float x = float(pieces[X]);
 float y = float(pieces[Y]);
 
 return new Lecturer(department, name, x, y);
}

class Lecturer {
 String department;
 String name;
 float x;
 float y;
 
 
 public Lecturer(String department, String name, float x, float y) {
   this.department = department;
   this.name = name;
   this.x = x;
   this.y = y;
 }
}


and here is the latest error:
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 3



Sorry for the huge posts
Thanks in advance for you time
Re: Reading in a database
Reply #1 - Feb 21st, 2010, 5:49am
 
Mmm, a CSV file represents an amount of data, but it isn't really a database, the term is more used for softwares like Access, MySQL or Oracle...

Anyway, when reporting an error, it is better to leave information like the start of the stack trace, indicating where the error happens.

The code you show seems OK with a quick glance. The problem might be in the data you don't show...
You must ensure that totalCount is correctly filled in the first line and that this first line has 5 comma-separated fields (including that totalCount).
And that each next line has 4 tab separated fields.
Re: Reading in a database
Reply #2 - Feb 21st, 2010, 12:40pm
 
Thanks for your reply, Im still trying to figure out what im doing but im very new to processing.
Im still getting the error
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 4
     at sketch_feb21a.parseInfo(sketch_feb21a.java:53)
     at sketch_feb21a.readData(sketch_feb21a.java:38)
     at sketch_feb21a.setup(sketch_feb21a.java:33)
     at processing.core.PApplet.handleDraw(PApplet.java:1402)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)


its highlighting the line:
maxY = float(infoPieces[4]);


im trying to read in an excel file saved as lecturer.csv
which contains:

name      x      y      department
CD      6      4      it
NM      5      4      maths

is it a problem with the file?  Huh
Re: Reading in a database
Reply #3 - Feb 21st, 2010, 2:08pm
 
Yes. The file you are trying to read seems to be missing the header line mentioned here:

Quote:
parseInfo(lines[0]); // read the header line


Try reading this file (notice it's structure is slightly different):

Quote:
#2,3,6,4,7
MATH      3      4      Mr.Fibbles
SCIENCE      6      7      Mrs.Bumpkinikini
Re: Reading in a database
Reply #4 - Feb 21st, 2010, 10:19pm
 
Quote:
is it a problem with the file?

Yes, or more exactly with the code trying to read that file. It must be adapted to its content.
Code:
void readData() {
String[] lines = loadStrings("lecturer.csv");
// Skip the header line
totalCount = lines.length - 1;
lecturers = new Lecturer[totalCount];
for ( int i = 1; i < lines.length; i++){
lecturers[lecturerCount] = parseLecturer(lines[i]);
lecturerCount++;
}
}
You can then drop the parseInfo function.
Re: Reading in a database
Reply #5 - Feb 22nd, 2010, 5:57am
 
Thanks for the replies. I changed the readData function like you said but I got the error saying cannot find Class named lecturer, so I moved the Lecturer class to the top of my file. I then realised the example I was following was a .tsv file and mine was a csv making this line of my code wrong (I think):
String pieces[] = split(line, TAB );

So I now changed my excel file to lecturer.txt tsv file and my code now looks like this:

class Lecturer {
 String department;
 String name;
 float x;
 float y;
 
 
 public Lecturer(String department, String name, float x, float y) {
   this.department = department;
   this.name = name;
   this.x = x;
   this.y = y;
 }
}
static final int DEPARTMENT = 1;
static final int X = 2;
static final int Y = 3;
static final int NAME = 4;

int totalCount;  // total number of lecturers
Lecturer[] lecturers;
int lecturerCount;


// min/max boundary of all points
float minX, maxX;
float minY, maxY;


public void setup() {
 readData();
}

void readData() {
 String[] lines = loadStrings("lecturer.txt");
  // Skip the header line
 totalCount = lines.length - 1;
 lecturers = new Lecturer[totalCount];
 for ( int i = 1; i < lines.length; i++){
   lecturers[lecturerCount] = parseLecturer(lines[i]);
 lecturerCount++;
 }
}


Lecturer parseLecturer(String line) {
 String pieces[] = split(line, TAB );
 String department = pieces[DEPARTMENT];
 String name = pieces[NAME];
 float x = float(pieces[X]);
 float y = float(pieces[Y]);
 
 return new Lecturer(department, name, x, y);
}


Suprisingly  Tongue it still doesnt work and is giving me the error
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 4
     at test.parseLecturer(test.java:65)
     at test.readData(test.java:56)
     at test.setup(test.java:47)
     at processing.core.PApplet.handleDraw(PApplet.java:1402)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)


All Im trying to do is get the program to load in the information and later im going to work on mapping it to the screen. Can anyone please suggest a simplier way of doing this??  Cry
Re: Reading in a database
Reply #6 - Feb 22nd, 2010, 6:27am
 
I meant to say that the error is highlighting the line:
String name = pieces[NAME];

Once again sorry for all the posts
Re: Reading in a database
Reply #7 - Feb 22nd, 2010, 7:04am
 
Problem is: Code:
static final int DEPARTMENT = 1;
static final int X = 2;
static final int Y = 3;
static final int NAME = 4;

First version, starting at zero, was correct...
Re: Reading in a database
Reply #8 - Feb 22nd, 2010, 7:19am
 
I realised that mistake after posting and changed it to 0,1,2,3 , but its still giving me the same error for the same line Embarrassed
Re: Reading in a database
Reply #9 - Feb 25th, 2010, 11:09pm
 
Hi.
Did you solve that problem ?
Please check that input data file is properly separated with TAB not SPACE.
Page Index Toggle Pages: 1