returning value in a loop, function
in
Programming Questions
•
1 month ago
Hi,
I have a hard time getting my head around functions and returning values ... and scope.
I am trying to return a value ("lyric"), inside a loop, using if statements.
For some reason I can only get the "lyric" String when it's inside the If statement,,, and thus can not return it at the end of the get_lyrics function...
When I try to call "lyric" outside the IF statement, it is Null....
Any help?
Here is the code I'm having problems with. Full code is at the bottom.
- GET URL
- //========================================================
- String get_lyrics () {
- // search base
- String baseURL = "http://www.lyricfind.com/services/lyrics-search/try-our-search/?q=";
- String url_request = baseURL + movie_keywords[0] + movie_keywords[1]+ movie_keywords[2];
- println("request FIRST =" +url_request);
- try {
- // println("request SECOND =" +url_request);
- String lines[] = loadStrings(url_request);
- println("There are " + lines.length + " lines in text search, in case you were interested");
- print("Printing lines 253..." +lines[253]);
- // String lyric = lines[252];
- for (int i = 253; i < 298; i =i+3){
- // replace <p style="margin-left:25px;"> etc.
- lines[i] = lines[i].replace("<p style=\"margin-left:25px;\">", "");
- lines[i] = lines[i].replace("<em>", " ");
- lines[i] = lines[i].replace("</em>", " ");
- lines[i] = lines[i].replace("<h2>", "");
- lines[i] = lines[i].replace("</p>", "...");
- lines[i]= lines[i].toUpperCase();
- // convertn keyword images to word find search
- String word2find1 = movie_keywords[0];
- String word2find2 = movie_keywords[1];
- String word2find3 = movie_keywords[2];
- // convert to upper case to bypass case-sensitice search
- word2find1 = word2find1.toUpperCase();
- word2find2 = word2find2.toUpperCase();
- word2find3 = word2find3.toUpperCase();
- if(lines[i].contains(word2find1) && lines[i].contains(word2find2) && lines[i].contains(word2find3) ){
- println(i + " triple winner " );
- println("triple winner= " +lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find2) && lines[i].contains(word2find3) ){
- println(i + " double WINNER 2 3 " );
- //println(lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find1) && lines[i].contains(word2find3) ){
- println(i + " double WINNER 1 3 " );
- //println(lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find1) && lines[i].contains(word2find2) ){
- println(i + " double WINNER 1 2" );
- //println(lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find1) || lines[i].contains(word2find2) ){
- String lyrics = lines[i];
- }
- else {
- //println("single match lyrics = " +lines[i]);
- String lyrics = lines[253];
- println("single match lyrics = " +lyrics);
- println ("these are the returned lyrics INSIDE= "+lyrics);
- }
- println ("these are the returned lyrics END OF LOOP= "+lyrics); // fails
- }// end for loop
- //String lyrics = lines[252];
- //
- }
- catch (Exception e){
- println("Sorry, comedy53 came back with no good matches. Please retry.");
- }
- println ("these are the returned lyrics= "+lyrics);
- return lyrics;
- } // end get Lyrics fuynction
- //========================================================
- // INTRO
- //========================================================
- /*
- Comedy53 is a Image+Text generator that matches images from popular film to lyrics, in hopes to create
- a humourous comic output.
- Comedy53 scrapes images from http://film-grab.com/, and uses keywords from those images to search the online
- lyric database http://www.lyricfind.com/services/lyrics-search/ to retrieve lyrics based on those keywords
- Keywords from the Movie can also be used to complete search.
- The images are then rearranged to match the stanza layout of the lyrics, to create a 3 frame comic.
- All images are provided from from http://film-grab.com/ == thank you ME
- All lyircs are provided from http://www.lyricfind.com/services/lyrics-search/ == thank you Roy
- created by dan hawkins
- School of Interactive Art + Technology, SFU
- drhawkin@sfu.ca
- April 2nd 2013
- */
- //========================================================
- // GLOBAL VARIABLES
- //========================================================
- //import libraries
- import prohtml.*;
- import java.util.*;
- PImage webImg1, webImg2, webImg3;
- HtmlImageFinder htmlImageFinder;
- String [] nothing = {""};
- ArrayList words1, words2, words3; // holds word search matches
- PFont font;
- PImage [] movie_images;
- String [] movie_keywords;
- int [] random_3_numbers;
- String url_request;
- String lyrics;
- int imgW = 512; // widthy of the image stills
- int imgH = 238; // height of the image stills
- // Ai, Bi, Ci, are used for re-ordering images to match stanza position
- // Ai = 1st order, Bi = 2nd order, Ci = 3rd order
- int Ai = 0;
- int Bi = 238;
- int Ci = 238*2;
- void setup () {
- background(20);
- font = loadFont("MyriadPro-Bold-24.vlw");
- size(imgW,imgH*3);
- intro();
- }
- void draw () {
- //println("drawdraw");
- }//end Draw
- // intro message
- void intro () {
- textFont(font); // myriad font
- textSize(40);
- fill(250);
- textAlign(CENTER);
- text("Welcome to comedy53. Press ENTER to begin", 10, (imgH*2)- 150, width-10, imgH*3);
- textFont(font, 24); //font
- } // end intro
- //**************************************************
- // USER INPUT : KeyBoard
- //**************************************************
- void keyPressed () {
- if (keyCode == ENTER) {
- random_3_numbers = getRandom_Numbers();
- movie_images = getMovieImages();
- movie_keywords = getMovieKeywords();
- //get_URL();
- get_lyrics();
- processLyrics();
- play();
- }
- } // end key pressed
- //**************************************************
- // Get 3 random numbers
- //**************************************************
- int [] getRandom_Numbers () {
- int rand15 = int(random(1,20));
- int rand30 = int(random(20,40));
- int rand45 = int(random(40,59));
- int [] random_3_numbers= {rand15, rand30, rand45} ;
- return random_3_numbers;
- }
- //**************************************************
- // GET MOVIE IMAGES
- //**************************************************
- PImage [] getMovieImages () {
- println("===========Confirmed. comedy53 is engaged...");
- //1) Select Movie
- int randomNumber = int(random(25));
- String [] movies = new String [26];
- //these movie URLs are loaded manually, rather than scraped from film-grab
- // because some films contain NO label/keywords attached to the image
- movies [0] = "http://film-grab.com/2010/11/23/american-beauty/";
- movies [1] = "http://film-grab.com/2010/06/23/blade-runner/";
- movies [2] = "http://film-grab.com/2010/07/06/2001-a-space-odyssey/";
- movies [3] = "http://film-grab.com/2010/07/07/a-clockwork-orange/";
- movies [4] = "http://film-grab.com/2010/10/04/brazil/";
- movies [5] = "http://film-grab.com/2010/07/19/the-darjeeling-limited/";
- movies [6] = "http://film-grab.com/2010/10/02/the-dark-knight/";
- movies [7] = "http://film-grab.com/2010/09/25/easy-rider/";
- movies [8] = "http://film-grab.com/2011/01/01/eternal-sunshine-of-the-spotless-mind/";
- movies [9] = "http://film-grab.com/2010/11/14/fight-club/";
- movies [10] = "http://film-grab.com/2010/07/27/the-godfather/";
- movies [11] = "http://film-grab.com/2010/09/01/halloween/";
- movies [12] = "http://film-grab.com/2010/09/21/lost-in-translation/";
- movies [13] = "http://film-grab.com/2011/01/12/moon/";
- movies [14] = "http://film-grab.com/2010/08/22/the-new-world/";
- movies [15] = "http://film-grab.com/2010/09/05/o-brother-where-art-thou/";
- movies [16] = "http://film-grab.com/2010/06/25/the-prestige/";
- movies [17] = "http://film-grab.com/2010/07/09/the-shining/";
- movies [18] = "http://film-grab.com/2010/08/16/there-will-be-blood/";
- movies [19] = "http://film-grab.com/2010/09/07/this-is-england/";
- movies [20] = "http://film-grab.com/2010/08/10/where-the-wild-things-are/";
- movies [21] = "http://film-grab.com/2010/08/24/the-wrestler/";
- movies [22] = "http://film-grab.com/2010/07/29/taxi-driver/";
- movies [23] = "http://film-grab.com/2010/10/14/rushmore/";
- movies [24] = "http://film-grab.com/2010/07/21/days-of-heaven/";
- movies [25] = "http://film-grab.com/2011/01/26/butch-cassidy-and-the-sundance-kid/";
- println("comedy53 is now looking for a movie...");
- println ("Movie selected: movie ID= " + randomNumber);
- // random numbers to get random images
- //2) Select Movie KeyWord
- String keyWmovie = movies[randomNumber];
- String [] keyWm1 = split(keyWmovie, ".com");
- String [] keyWm2 = split(keyWm1[1], "/");
- String [] keyWm3 = split(keyWm2[4], "/");
- String[] movieTemp = concat(keyWm3, nothing);
- String movieKeyword= join (movieTemp, "+");
- //insert your url here
- htmlImageFinder = new HtmlImageFinder(movies[randomNumber]);
- PImage[] images = new PImage[htmlImageFinder.getNumbOfImages()];
- //println("Movie contains this many images=" +htmlImageFinder.getNumbOfImages());
- println ("%&& computing&**/ systemcheck: Does Chris Penney Suck? @&%&* error");
- println ("ID_e404. System confirmed: Yes, Chris Penney does suck. $!running_cont");
- try{
- for(int i = 0;i<50;i++){
- images[i] = loadImage(htmlImageFinder.getImageLink(i));
- images[i].resize(imgW,imgH);
- // println(htmlImageFinder.getImageLink(i));
- }//end LOOP
- } // end try
- catch (NullPointerException e) {
- }
- // make images better resolution, remove ?w=150& from URL
- String betterPic1 = htmlImageFinder.getImageLink(random_3_numbers[0]);
- String[] picture1 = split(betterPic1,"?w=150&");
- String betterPic2 = htmlImageFinder.getImageLink(random_3_numbers[1]);
- String[] picture2 = split(betterPic2,"?w=150&");
- String betterPic3 = htmlImageFinder.getImageLink(random_3_numbers[2]);
- String[] picture3 = split(betterPic3,"?w=150&");
- println("This is picture 1..." + picture1[0]);
- println("This is picture 2..." + picture2[0]);
- println("This is picture 3..." + picture3[0]);
- String url1 = picture1[0];
- String url2 = picture2[0];
- String url3 = picture3[0];
- // Load image from a web server
- webImg1 = loadImage(url1, "gif");
- webImg1.resize(imgW,imgH);
- webImg2 = loadImage(url2, "gif");
- webImg2.resize(imgW,imgH);
- webImg3 = loadImage(url3, "gif");
- webImg3.resize(imgW,imgH);
- PImage []movie_images = {webImg1, webImg2,webImg3};
- return movie_images;
- }
- //**************************************************
- // GET MOVIE KEY WORDS
- //**************************************************
- // clean up image, extract keyword -- .png, etc
- String [] getMovieKeywords () {
- String kW1a = htmlImageFinder.getImageLink(random_3_numbers[0]);
- String[] kW1b = split(kW1a,".png");
- String[] kW1c = split (kW1b[0], "-");
- String [] kW1temp= split (kW1c[1], "1");
- String[] kW1final = concat(kW1temp, nothing);
- String kW2a = htmlImageFinder.getImageLink(random_3_numbers[1]);
- String[] kW2b = split(kW2a,".png");
- String[] kW2c = split (kW2b[0], "-");
- String[] kW2temp= split (kW2c[1], "1");
- String[] kW2final = concat(kW2temp, nothing);
- String kW3a = htmlImageFinder.getImageLink(random_3_numbers[2]);
- String[] kW3b = split(kW3a,".png");
- String[] kW3c = split (kW3b[0], "-");
- String[] kW3temp= split (kW3c[1], "1");
- String[] kW3final = concat(kW3temp, nothing);
- //========================================================
- // GET KEYWORDS AND SEARCH
- //========================================================
- // add + to end of Keyword to put into URL search
- String keyword1temp= join( kW1final, "+");
- String keyword1 = keyword1temp;
- String keyword2temp= join( kW2final, "+");
- String keyword2 = keyword2temp;
- String keyword3temp= join( kW3final, "+");
- String keyword3 = keyword3temp;
- //========================================================
- // MAKE KEKWORD IMAGE detectable, remove +
- String kw1image = keyword1;
- if( kw1image.charAt( kw1image.length()-1) == '+' ){
- kw1image = kw1image.substring( 0, kw1image.length()-1 );
- }
- String kw2image = keyword2;
- if( kw2image.charAt( kw2image.length()-1) == '+' ){
- kw2image = kw2image.substring( 0, kw2image.length()-1 );
- }
- String kw3image = keyword3;
- if( kw3image.charAt( kw3image.length()-1) == '+' ){
- kw3image = kw3image.substring( 0, kw3image.length()-1 );
- }
- println("Image 1="+kw1image+", Image 2="+kw2image+", Image 3="+kw3image);
- String [] movie_keywords = {keyword1, keyword2, keyword3};
- return movie_keywords;
- }
- //========================================================
- // GET URL
- //========================================================
- String get_lyrics () {
- // search base
- String baseURL = "http://www.lyricfind.com/services/lyrics-search/try-our-search/?q=";
- String url_request = baseURL + movie_keywords[0] + movie_keywords[1]+ movie_keywords[2];
- println("request FIRST =" +url_request);
- try {
- // println("request SECOND =" +url_request);
- String lines[] = loadStrings(url_request);
- println("There are " + lines.length + " lines in text search, in case you were interested");
- print("Printing lines 253..." +lines[253]);
- // String lyric = lines[252];
- for (int i = 253; i < 298; i =i+3){
- // replace <p style="margin-left:25px;"> etc.
- lines[i] = lines[i].replace("<p style=\"margin-left:25px;\">", "");
- lines[i] = lines[i].replace("<em>", " ");
- lines[i] = lines[i].replace("</em>", " ");
- lines[i] = lines[i].replace("<h2>", "");
- lines[i] = lines[i].replace("</p>", "...");
- lines[i]= lines[i].toUpperCase();
- // convertn keyword images to word find search
- String word2find1 = movie_keywords[0];
- String word2find2 = movie_keywords[1];
- String word2find3 = movie_keywords[2];
- // convert to upper case to bypass case-sensitice search
- word2find1 = word2find1.toUpperCase();
- word2find2 = word2find2.toUpperCase();
- word2find3 = word2find3.toUpperCase();
- if(lines[i].contains(word2find1) && lines[i].contains(word2find2) && lines[i].contains(word2find3) ){
- println(i + " triple winner " );
- println("triple winner= " +lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find2) && lines[i].contains(word2find3) ){
- println(i + " double WINNER 2 3 " );
- //println(lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find1) && lines[i].contains(word2find3) ){
- println(i + " double WINNER 1 3 " );
- //println(lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find1) && lines[i].contains(word2find2) ){
- println(i + " double WINNER 1 2" );
- //println(lines[i]);
- String lyrics = lines[i];
- }
- else if(lines[i].contains(word2find1) || lines[i].contains(word2find2) ){
- String lyrics = lines[i];
- }
- else {
- //println("single match lyrics = " +lines[i]);
- String lyrics = lines[253];
- println("single match lyrics = " +lyrics);
- println ("these are the returned lyrics INSIDE= "+lyrics);
- // processLyrics (String lyrics);
- // exit();
- }
- println ("these are the returned lyrics END OF LOOP= "+lyrics); // fails
- }// end for loop
- //String lyrics = lines[252];
- //
- }
- catch (Exception e){
- println("Sorry, comedy53 came back with no good matches. Please retry.");
- }
- println ("these are the returned lyrics= "+lyrics);
- return lyrics;
- } // end get Lyrics fuynction
- //========================================================
- // COMBINE and Make Comics
- //========================================================
- void processLyrics () {
- println("this is it man! = " +get_lyrics());
- }
- void play () {
- println("Movie contains this many images=" +htmlImageFinder.getNumbOfImages());
- image(movie_images[0], 0, Ai);
- image(movie_images[1], 0, Bi);
- image(movie_images[2], 0, Ci);
- println("lyrics are..."+lyrics);
- }
1