2 More String Questions

edited December 2015 in How To...

I've solved how to add numerous random parts of speech into my string stories, but I have run into a couple of issues. Most importantly: I want my code to remember one of the random names: i.e. "(random name) likes (random noun), but (same random name) also likes (random noun)."

Additionally, I have been able to output a couple of different stories (via separate text documents) that say different things, but still use the random parts of speech aspect, but story2 also says "null" below what it outputs in the text document. How do I fix this?

I'm far more concerned about the name remembrance than the "null" issue. Code is below: Any ideas?

String[] nouns={"house", "car", "cat"}; 
String[] verbs={"jumps", "runs", "tells"   }; 
String[] adjectives={"yellow", "adorable", "helpful"      };
String[] names={"Tiger", "Phil", "Jack", "Arnold"         };
int fileCount = 0;

String storyTemplate1 = "One day %name% was %verb% by with a walkman on when %name% saw a %noun% giving him an %adjective% eye and I %verb% him off in the parking lot. I don't give a damn if it's dark or not, I'm harder than me trying to park a %noun% .";
String storyTemplate2 = " %name% %verb% %adjective% %noun% .";

void setup() {
  size(200, 200); 
  nouns = loadStrings("Nouns.txt"); 
  verbs = loadStrings("Verbs.txt"); 
  adjectives = loadStrings("Adjectives.txt"); 
  names = loadStrings("Names.txt");

  String story1 = instantiateStory(storyTemplate1); 
  String story2 = buildStory(storyTemplate2);
  String[] output = new String[1]; 
  String[] output2 = new String[2];
  output[0] = story1;
  output2[0] = story2; 
  saveStrings("story1.txt", output);
  saveStrings("story2.txt", output2); 
}

String instantiateStory(String template) { 
  String story = ""; 
  String[] splitTemplate = split(template, " ");
  for (int i = 0; i < splitTemplate.length; i++) { 
    // 
    if (splitTemplate[i].equals("%noun%")) { 
      //Adds a random noun to story 
      story = story + getRandomNoun() + " ";
    } else if (splitTemplate[i].equals("%verb%")) { 
      story = story + getRandomVerb() + " ";
    } else if (splitTemplate[i].equals("%adjective%")) {
      story = story + getRandomAdjective() + " ";
    }
     else if (splitTemplate[i].equals("%name%")) {
      story = story + getRandomName() + " ";
    }
    else {
      story = story + splitTemplate[i] + " ";
    }
  }  
  return story;
}

String buildStory(String template2) { 
  String story2 = ""; 

  String[] splitTemplate2 = split(template2, " ");
  for (int i = 0; i < splitTemplate2.length; i++) { 
    if (splitTemplate2[i].equals("%noun%")) { 
      //Adds a random noun to story 
      story2 = story2 + getRandomNoun() + " ";
    } else if (splitTemplate2[i].equals("%verb%")) { 
      story2 = story2 + getRandomVerb() + " ";
    } else if (splitTemplate2[i].equals("%adjective%")) {
      story2 = story2 + getRandomAdjective() + " ";
    }
     else if (splitTemplate2[i].equals("%name%")) {
      story2 = story2 + getRandomName() + " ";
    }
    else {
      story2 = story2 + splitTemplate2[i] + " ";
    }
  }  
  return story2;
}

String getRandomNoun() { 
  int randomIndex = (int)random(0, nouns.length); 
  return nouns[randomIndex];
}

String getRandomVerb() { 
  int randomIndex = (int)random(0, verbs.length); 
  return verbs[randomIndex];
}

String getRandomAdjective() {
  int randomIndex = (int)random(0, adjectives.length);
  return adjectives[randomIndex];
}

String getRandomName() {
  int randomIndex = (int)random(0, names.length);
  return names[randomIndex];
}

Answers

  • please format your code.

    highlight, hit ctrl-o...

  • Much better, thanks.

  • edited December 2015 Answer ✓

    If you want only one name instead of multiple random names, just pick that one name at the beginning, like:String myName = getRandomName();.
    Then in your loop, add this name instead of a new random one.

    ...
    
    else if (splitTemplate2[i].equals("%name%")) {
        story2 = story2 + myName + " ";
    }
    
    ...
    

    I didn't see a null-output for your second story, but i did not really look into it yet.

  • Just curious, is there any difference between your function buildStory() and instantiateStory() ? They seem to do the same thing.

  • Thank you. And no, processing was just giving me an error when I was trying to run it I had to change it.

Sign In or Register to comment.