Why is noun1 not resolving to a variable?

String[] nouns;
String[] verbs;
String[] adjectives;
//int fileCount= 0;


String storyTemplate1= "I really like %noun%";
String storyTemplate2= "It all began when %name1% %verb% with %noun%";

void setup(){
  size(600,600);

  nouns= loadStrings("Nouns.txt");
  verbs= loadStrings("Verbs.txt");
  adjectives= loadStrings("Adjectives.txt");
  //for(int i=0; i<nouns.length; i++){
  //  println(nouns[i]);
  //}

  String story1= instantiateStory(storyTemplate1) + instantiateStory(storyTemplate2);
  String[] output= new String[1];
  output[0]= story1;
  saveStrings("story1.txt", output);


}


//void mousePressed(){
//  int randomIndex= (int)random(0, nouns.length);
//  String[] output= new String[1];
//  output[0]= nouns[randomIndex];
//  saveStrings(fileCount +".txt", output);
//  fileCount++;
//}


void draw(){

}

String noun1(String noun1){
     noun1 = " ";
    int randomIndex= (int)random(0, nouns.length);
      String[] output= new String[1];
      output[0]= nouns[randomIndex];
       noun1= nouns[randomIndex];
       return noun1;
}

String instantiateStory(String template){
  String story= " ";


  String[] splitTemplate= split(template, " ");
  for( int i=0; i<splitTemplate.length; i++){

   if(splitTemplate[i].equals("%noun%")){

      story = story + noun1 + " ";
   }

   else if(splitTemplate[i].equals("%verb%")){
     randomIndex= (int)random(0, nouns.length);
      output[0]= verbs[randomIndex];
      story = story + verbs[randomIndex] + "ed" + " ";
   }
   else if(splitTemplate[i].equals("%name1")){
     randomIndex= (int)random(0, nouns.length);

      story = story + "Harry" + " ";
   }
   else{
     story = story + splitTemplate[i] + " ";
   }
  }

  return story;
}

Answers

  • edited December 2015 Answer ✓

    "noun1" is both method noun1() & its parameter noun1.
    Within instantiateStory() @ line #60: story = story + noun1 + " ";
    I believe you've meant to invoke "noun1" as method noun1()? :-/

  • here else if(splitTemplate[i].equals("%name1")){

    you want %name1%

    2nd % missing

Sign In or Register to comment.