Pergunta

I'm having some brain issues today, and trying to code with a toddler running around the house isn't helping. I feel like this is something so obvious that I should have had it in 5 minutes, but it's been an hour, and I don't get it. I'm going to take a break and come back to look at it, but I'm hoping in the mean time someone can point out the obvious that I'm missing.

I have a method in one class that returns a String array. I want that returned array to be assigned to a String array in the method of the other class that is accepting the return, but it appears to not be working.

Here's the example code:

String[] meta = Scraper.scrape(movie);
    
            
    for(String i: meta) 
    {
        if (i == null)
            {
                continue;
            }
        JOptionPane.showMessageDialog(null, i);

    }

The values in the String array are meta data from a movie (release year, description, title, etc.) Nothing long or fancy, and the for loop shown above works fine in the scrape method to display the data that is in the returning String array, but it is doing nothing when I try to examine the data in the returned array in the calling method (assuming because the data is null after the return, and further assuming that I'm doing something wrong in assigning the returned array to the new array).

Someone help give my brain a kick start today. Thanks!

UPDATE - adding Scraper class example:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Scraper {

public static String[] scrape(String movieTitle)
{
    String[] results = new String[20];
    results[0] = movieTitle;
    movieTitle.replace(" ", "+");       
    // example: http://www.omdbapi.com/?i=&t=Short+Circuit
    String queryURL = "http://www.omdbapi.com/?i=&t=" + movieTitle;
    WebDriver driver = new ChromeDriver();
    driver.get(queryURL);
    String page = driver.getPageSource();

    Scanner scanner = new Scanner(page);
    results[0] = page.substring(page.indexOf("Title") + 8, page.indexOf("\"", page.indexOf("Title") + 9));  // Title
    results[1] = page.substring(page.indexOf("Year") + 7, page.indexOf("Year") + 11);  // Year released
    results[2] = page.substring(page.indexOf("Rated") + 8,  page.indexOf("\"", page.indexOf("Rated") + 9));  //  Rating
    results[3] = page.substring(page.indexOf("Released") + 11,  page.indexOf("\"", page.indexOf("Released") + 12));  //  Release Date
    results[4] = page.substring(page.indexOf("Runtime") + 10,  page.indexOf("\"", page.indexOf("Runtime") + 11));  //  Running Time
    results[5] = page.substring(page.indexOf("Genre") + 8,  page.indexOf("\"", page.indexOf("Genre") + 9));  //  Genre
    results[6] = page.substring(page.indexOf("Director") + 11,  page.indexOf("\"", page.indexOf("Director") + 12));  //  Director(s)
    results[7] = page.substring(page.indexOf("Writer") + 9,  page.indexOf("\"", page.indexOf("Writer") + 10));  //  Writer(s)
    results[8] = page.substring(page.indexOf("Actors") + 9,  page.indexOf("\"", page.indexOf("Actors") + 10));  //  Starring
    results[9] = page.substring(page.indexOf("Plot") + 7,  page.indexOf("\"", page.indexOf("Plot") + 8));  //  Starring
    results[10] = page.substring(page.indexOf("Poster") + 9,  page.indexOf("\"", page.indexOf("Poster") + 10));  //  Plot
    
    scanner.close();
    

    driver.close();
    return results;
    
    
}

}

I had previously verified that the results returned were not null or empty using the same for loop that I have now have in calling method to verify data. The for loop is basically just there to sanity check me and ensure that I'm not missing something obvious (like parsing from the wrong index or something) and that the proper data is returned as I expect.

Foi útil?

Solução

Not sure if this will cause a problem for your URL query, but you're not using .replace() correctly. You're currently trying use it to modify an immutable String. Remember .replace() does not modify the String, but it will return a new modified String. So instead of this:

movieTitle.replace(" ", "+");       <---
String queryURL = "http://www.omdbapi.com/?i=&t=" + movieTitle;

Try and use this

movieTitle = movieTitle.replace(" ", "+");

Also, I'm not sure what your using the scanner for, but you're not actually using it.

Outras dicas

Im wondering if it may have something to do with the fact that you are using a static reference? To be honest I can not see a real reason why this would not work they way you have it. EDIT: I just tried several different ways of doing this with two classes using both static and non static methods and had no issues. Maybe you could show more code from the calling class that is receiving the returned array.

The answer goes back to my brain not being turned on. I had set the default value of the "Movie Title" in code for testing. However, I set it BEFORE I had set the variable that is entered in the GUI (which defaults to "Movie Title"). So I set it, the GUI reset it, and for some reason the site I query was unable to find "Movie Title" as a valid movie title in their database.

Basically, I was busy being distracted and dumb, and failed to see what was right in front of me the whole time.

Thank you all for attempting to solve my stupidity.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top