ikhebgeenaccount
ikhebgeenaccount

Reputation: 373

After splitting a string, ArrayOutOfBoundsException

Let's say that url = "http://gmail.com".
I try to make from this a string dnsname = "Top-level com".

Now we run the following piece of code:

System.out.println(url);
String[] urlsplit = new String[3];
System.out.println(url.substring(10,11));
urlsplit = url.split(url.substring(10,11));
String dnsname = "Top-level " + urlsplit[urlsplit.length - 1];
System.out.println(dnsname);

As output we get:

http://www.gmail.com
.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1

I do not see the mistake I made, however there must be one.

Upvotes: 1

Views: 114

Answers (4)

SparkOn
SparkOn

Reputation: 8946

The problem is that String#split receives a regular expression, the dot (.) symbol is a special character in regular expressions, so you need to send it as a plain symbol.

Replace

urlsplit = url.split(url.substring(10,11));

with

urlsplit = url.split("\\"+url.substring(10,11));

Or simply do it this way

urlsplit = url.split("\\.");

But the question is why ArrayOutOfBoundsException?

Because of the wrong pattern in your split function the array never got populated. And you were trying to access it second index's value so in that case it gave you an ArrayOutOfBoundsException

Upvotes: 0

shlomi33
shlomi33

Reputation: 1568

This is the best way to avoid the need to remember all the regex meta-characters:

String url = "http://gmail.com";
System.out.println(url);
String[] urlsplit = new String[3];
System.out.println(url.substring(12, 13));
urlsplit = url.split(Pattern.quote(url.substring(12, 13)));
String dnsname = "Top-level " + urlsplit[urlsplit.length - 1];
System.out.println(dnsname);

NOTE: you'll be better probably with:

urlsplit = url.split(Pattern.quote("."));

If you run this code you will get:

http://gmail.com
.
Top-level com

Upvotes: 0

jdphenix
jdphenix

Reputation: 15415

If I understand your question correctly, you want the output

Top-level com

If this is what you're wanting given a URL, Consider using the java.net.URL class along with split().

import java.net.MalformedURLException;
import java.net.URL;

public class Program {

    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://gmail.com/"); 
        System.out.println(url);

        String[] hostParts = url.getHost().split("\\.");

        String dnsname = "Top-level " + hostParts[hostParts.length - 1];
        System.out.println(dnsname);
    }

}

This outputs:

http://gmail.com
Top-level gmail.com

java.net.URL

This also has the benefit of working with URLs that aren't only the hostname, for example, given http://www.example.co.aq/query.exe?field=name&id=12#fragment, this will output Top-level aq.

Upvotes: 0

Dici
Dici

Reputation: 25950

I think the dot is considered as the regex pattern meaning "any character" so your split method returns an empty array. Just do the following :

String url = "http://gmail.com";
System.out.println(url);
//Escape the . to tell the parser it is the '.' char and not the regex symbol .
String[] urlsplit = url.split("\\.");
String dnsname = "Top-level " + urlsplit[urlsplit.length - 1];
System.out.println(dnsname);

If you run the same code using "." instead of "\." you will have the same problem as in your code. because s.split(".") actually returns an empty array so urlsplit.length - 1 is negative and urlsplit[urlsplit.length - 1] is obviously out of bounds.

Upvotes: 1

Related Questions