views:

79

answers:

3

I want to get a proxy list and parse it into an array of strings, txt is the proxy list, tmp[] is an array that has element in the form "ipaddr:port". ie. (tmp[] = {"i.p.i.p:port", "i.p.i.p:port", ...}). proxies array should be in 2d and look like this: {{"i.p.i.p","port"}, {"i.p.i.p","port"}, ...} but when running it java complains the following:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at Bot.init(Bot.java:64) at Bot.main(Bot.java:24)

String[] tmp = txt.split(";");
String[][] proxies = new String[tmp.length/2][2];
for(int i = 1; i<tmp.length; i=i+2){
    String[] proxy = tmp[i].split(":");
    for(int j = 0; j<tmp.length; j++){
        for(int k = 0; k<proxy.length; k++){
            proxies[j][k] = proxy[k];
        }
    }
}
+1  A: 

In Java array indexes start at 0 and not 1. Perhaps you meant int i = 0. Another thing is what is the format of the input? I do not think you meant i = i + 2.

Assuming that input is in the format ip:port[;ip:port]*.

String[] tmp = txt.split(";");
String[][] proxies = new String[tmp.length][2];
for(int i = 0; i<tmp.length; ++i){
    String[] proxy = tmp[i].split(":");
    proxies[i][0] = proxy[0];
    proxies[i][1] = proxy[1];
}

For the input 192.168.0.1:123;192.168.0.2:456 would create an array like:

{    
    { "192.168.0.1", "123" },
    { "192.168.0.2", "456" },
}

Validation code not included for sake of clarity.

smink
A: 

You always overwrite the whole old data with j-loop. If I understood you right, this loop isn't needed. And because you know the number of k-loop-iterations (ie 2) you don't need it either. Try something like this:

for(int i=0; i<tmp.length; i++) {
  String[] proxy = tmp[i].split(":");
  proxies[i][0] = proxy[0];
  proxies[i][1] = proxy[1];
}
Mafi
this wont work because the size of proxies is tmp.length/2 so the above code wont work, thats why i think i would need the j-loop
Tamer
+2  A: 

Beyond what smink has already said, I'd like to argue that it's a bad idea to use a two dimensional string array for storing data like this. Create a class to hold the connection details, or use one of the existing such as SocketAddress.

public static class ProxyConnectionDetail {
    public String host;
    public int port;

    public ProxyConnectionDetail(String host, int port) {
        this.host = host;
        this.port = port;
    }
}

...

String[] tmp = txt.split(";");
List<ProxyConnectionDetail> proxies = new ArrayList<ProxyConnectionDetail>();
for(int i = 0; i<tmp.length; ++i){
    String[] proxy = tmp[i].split(":");
    proxies.add(new ProxyConnectionDetail(proxy[0], Integer.parseInt(proxy[1])));
}

This has the advantage of being more extensible in case you ever want to add, say, authentication details. It also results in clearer code, and a smaller chance of unforseen runtime errors. You should always rely as much as possible on the type system rather than passing around low level data structures like string arrays.

Emil H
good point, but i only need it really temporarily so there is no need for a class to do it
Tamer