Search code examples
javaarraysstringreturnassign

Assign a returned String array to a new String array - Java


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.


Solution

  • 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.