0
votes
package com.bml.icbs.ws.param;    
import java.util.HashMap;

public class RequestParameter {

private String param;
private HashMap<String, String> hashmap;

    public RequestParameter(String param){

    this.param=param;
    this.hashmap = new HashMap<String, String>();

    Integer i; 
    String key,value;
    String myArray[] = this.param.split("&");


    for (i=0;i<myArray.length;i++){

        key=myArray[i].substring(0,myArray[i].indexOf("="));
        value=myArray[i].substring(myArray[i].indexOf("=")+1);
        this.hashmap.put(key,value);
    }
}

public String getParameterValue(String key){
    return this.hashmap.get(key);
}

public String getParam() {
    return param;
}

public void setParam(String param) {
    this.param = param;
}

public HashMap<String, String> getHashmap() {
    return hashmap;
}

public void setHashmap(HashMap<String, String> hashmap) {

    this.hashmap = hashmap;
}

}

When I Test the above method in case the URL is "param2=ffffhhh&param1=oooo&param3=pppp&param4=iiii&param5=kkkkk"

The result is: SIZE 5 URL param2=ffffhhh&param1=oooo&param3=pppp&param4=iiii&param5=kkkkk HASHMAP {param1=oooo, param2=ffffhhh, param3=pppp, param4=iiii, param5=kkkkk} PARAM1 = oooo

which is correct

  public static void main(String[] args) {
    RequestParameter reqParam = new RequestParameter("param2=ffffhhh&param1=oooo&param3=pppp&param4=iiii&param5=kkkkk");
    System.out.println("SIZE " + reqParam.getHashmap().size());
    System.out.println("URL " + reqParam.getParam());
    System.out.println("HASHMAP " + reqParam.getHashmap());
    System.out.println("PARAM1 = " reqParam.getParameterValue("param1"));

}

but In case I pass the URL as the following one: "param2=ffff&&&hhh=&param1=oooo=&&param3=pppp&param4=iiii&&yy=&param5=kkkkk"

public static void main(String[] args) {
    RequestParameter reqParam = new RequestParameter("param2=ffff&&&hhh=&param1=oooo=&&param3=pppp&param4=iiii&&yy=&param5=kkkkk");
    System.out.println("SIZE " + reqParam.getHashmap().size());
    System.out.println("URL " + reqParam.getParam());
    System.out.println("HASHMAP " + reqParam.getHashmap());
    System.out.println("PARAM1 = " reqParam.getParameterValue("param1"));

}

it throws the following exception: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1937) at com.bml.icbs.ws.param.RequestParameter.(RequestParameter.java:188) at com.bml.icbs.ws.param.main.main(main.java:9)

1
This happens because you can´t create a substring of an empty String that is beeing split by the "&". For better understanding, print out the information in your loop after splitting by the "&", you will notice having empty strings a few times, because of the repeating "&" at the beginning.SomeJavaGuy
@KevinEsche Thanks but do you have any solution?Aya
just ignore the empty string by using continue when it´s empty or don´t do anything if it´s emptySomeJavaGuy

1 Answers

1
votes

make sure array element !empy.

for (i=0;i<myArray.length;i++){
        if(myArray[i] == null || myArray[i].length()==0){
                continue;
        }
        key=myArray[i].substring(0,myArray[i].indexOf("="));
        value=myArray[i].substring(myArray[i].indexOf("=")+1);
        this.hashmap.put(key,value);
    }