0

I have made this small script to test something, but I can't get it working. I want it to replace the "%20" in the string to " ". This gets printed out: Ravenous%20Cache.zip

package test;

public class Test {

    public static void main(String[] args) {
        System.out.println(getArchivedName());
    }

    private static String cacheLink = "https://dl.dropboxusercontent.com/u/Ravenous%20Cache.zip";

    private static String getCacheLink() {
        return cacheLink;
    }

    private static String getArchivedName() {
        String name = cacheLink.replace("%20", " ");
        int lastSlashIndex = name.lastIndexOf('/');
        if (lastSlashIndex >= 0 
            && lastSlashIndex < getCacheLink().length() -1) { 
            return getCacheLink().substring(lastSlashIndex + 1);
        } else {
            System.err.println("Error Downloading Game Files.");
        }
        return "";
    }

}
2
  • That's because you are returning a substring of getCacheLink() instead of a substring of name. Commented May 28, 2015 at 16:11
  • Works just fine. As RealSkeptic said you are returning the value before it was replaced not after. Commented May 28, 2015 at 16:14

3 Answers 3

2

You're calling getCacheLink() again when you take the substring that gives you back the original string

return name.substring(lastSlashIndex + 1);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works. Everyone thanks for your posts and I'm sure that you're all right.
0

Try this:

public static void main(String[] args) {
        System.out.println(getArchivedName());
    }

    private static String cacheLink = "https://dl.dropboxusercontent.com/u/Ravenous%20Cache.zip";

    private static String getCacheLink() {
        return cacheLink;
    }

    private static String getArchivedName() {
        String name = cacheLink.replace("%20", " ");
        int lastSlashIndex = name.lastIndexOf('/');
        if (lastSlashIndex >= 0 
            && lastSlashIndex < getCacheLink().length() -1) { 
            return name.substring(lastSlashIndex + 1);//Changed
        } else {
            System.err.println("Error Downloading Game Files.");
        }
        return "";
    }

You were returning getCacheLink() not name.

Comments

0
public class Test {

    public static void main(String[] args) {
        System.out.println(getArchivedName());
    }

    private static String cacheLink = "https://dl.dropboxusercontent.com/u/Ravenous%20Cache.zip";

    private static String getCacheLink() {
        return cacheLink;
    }

    private static String getArchivedName() {
        cacheLink = cacheLink.replace("%20", " ");
        int lastSlashIndex = cacheLink.lastIndexOf('/');
        if (lastSlashIndex >= 0 && lastSlashIndex < getCacheLink().length() - 1) {
            return getCacheLink().substring(lastSlashIndex + 1);
        } else {
            System.err.println("Error Downloading Game Files.");
        }
        return "";
    }
}

1 Comment

Ravenous Cache.zip is what you get after running this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.