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 › difficulties with arrays
Page Index Toggle Pages: 1
difficulties with arrays (Read 478 times)
difficulties with arrays
Dec 27th, 2007, 8:43pm
 
Hi all,
I'm doing my first babysteps with processing and am trying to build an application to drive the 'grote grijze muizenshow' a quiz thingy me and my GF came up with for new years eve.

I have this database with questions and answers out of wich I have to choose random questions. So it's an array like the example I found on the site. I have a random function
r=ceil(random(firstName.lenght))-1; to get my questions.
But I want each random function to go into an array, so I can check if the question (=randomNumber) has already been chosen.
But I get a really strange array if I print it(~2mia/c  lijst[I@c7cb793)

here is the code so far (remember babysteps ;-)

String[]firstName={"jan","bob","mia","pieter","marijke","joris","ivan","bram","veerle","
ellen","kim"};
String[]lastName={"a","b","c","d","e","f","g","h","i","j","k","l"};
int[]lijst=new int[0];
int sielR=ceil(0);
int mouseCheck=0;

void draw(){
}
void mousePressed() {
 int r=ceil(random(firstName.length))-1;
 lijst=append(lijst,r);
 print(r);
 print(firstName[r]+"/");
 print(lastName[r]+"  ");
 print("lijst"+lijst);
 mouseCheck ++;
 print(mouseCheck);
}

hope someone can give me some pointers
Re: difficulties with arrays
Reply #1 - Dec 28th, 2007, 4:23pm
 
Quote:
But I get a really strange array if I print it(~2mia/c  lijst[I@c7cb793)

yes, because you can't use println() with an array! you should try to replace the line print("lijst"+lijst); with the following code :

Code:
print("lijst : [");
for (int i = 0; i < lijst.length; i++) {
print(lijst[i]);
if (i < lijst.length - 1) print(", ");
}
println("]");
Re: difficulties with arrays
Reply #2 - Dec 30th, 2007, 2:53am
 
to be precise, processings println() has array printing built in. But you must use println (not just print()), and only pass the list to it. so, replace

print("lijst"+lijst);

with

println("lijst:");
println(lijst);

Re: difficulties with arrays
Reply #3 - Dec 30th, 2007, 1:23pm
 
Thanx guys, it's a bit hard for me to see what I'm doing but while printing it's ok.

Now for the next part I want to shuffle an array, by choosing a random element in the first array and append() it to the next while removing it from the first.
Then generate a random(firstArray.length) element from the first again and append it to the next when the firstArray.length==0 saving the file and exiting the programm.
The thing is I can't seem to get the remove part which I copied from another script.
I know i should be learning this diffrently but the great grey mouse show (the quiz) is tomorrow so I'm begging for your help!

here's the script:


float rand =0;
int keuze= 0;

String[] lines;
String[] shuffleArray={"Vraag en antwoord"};
int recordCount;
PFont body;
int num = 1; // Display this many entries on each screen.
int startingEntry = 0;  // Display from this entry number

void setup()
{
 size(200, 200);
 fill(255);
}
void draw(){

 
 lines = loadStrings("kwisvragen.txt");
 if(lines.length!=0){
   int r=floor(random(lines.length));
   shuffleArray=append(shuffleArray,lines[r]);
   print(r+"   ");
   String[] lines(String[]vals, int index){
     vals=concat(subset(vals,0,index),subset(vals,index+1));
     return vals;
   }
 }else{
   println(shuffleArray);
   saveStrings("shuffle.txt",lines);
   exit();
 
}
}

   

Re: difficulties with arrays
Reply #4 - Dec 31st, 2007, 7:02am
 
to answer my own questions here is the script that worx (and then crashes processing) hehe:
thx everyone for helping me through the codingforest

String[] naam;
String[] verzamel={"vraag en antwoord"};
void setup(){
 naam = loadStrings("kwisvragen.txt");
}

void draw(){

 for(int i=0;i%100<=0;i++){
   if(i==0){
     if(naam.length>0){
       int r= ceil(random(naam.length-1));
       verzamel=append(verzamel,naam[r]);
       print(r);
       naam=concat(subset(naam,0,r),subset(naam,r+1));
       println(naam);
       
     }else{
       saveStrings("shuffle.txt",verzamel);
       exit();
     }
   }
 }
}

Re: difficulties with arrays
Reply #5 - Dec 31st, 2007, 7:40am
 
Quote:
I know i should be learning this diffrently

Indeed, I think it would have been easier to work with Java Collections, which provide lots of useful built-in methods :
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html
Page Index Toggle Pages: 1