1

I want to create a run time String name in Java.
I tried something like using in JavaScript, but it is printing value like Status_Name_0 instead Open assigned to the String Status_Name_0

 public static void GetPaymentStatusList(){
        int i=0;
        String Status_Name_0="Open";
        String Status_ID_0="0";
        String Status_Name_1="Approved";
        String Status_ID_1="1";
        String Status_Name_2="Denied";
        String Status_ID_2="2";
        for(i=0; i<3; i++){
            Vars.PaymentStatusName_List.add("Status_Name_"+i);
            Vars.PaymentStatusId_List.add("Status_ID_"+i);
        }
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

1

but it is printing value like Status_Name_0 instead Open

Because that's what you added to the list...

 add("Status_Name_"+i);

The way to get what you want would be a Map<String, String>

Map<String, String> map = new HashMap<>();
map.put("Status_Name_0", "Open");
// ...

for (int i=0;i<map.size();i++) {
    String open = map.get("Status_Name_"+i);
}

How about you make some class instead, though?

public class PaymentStatus {

    int id;
    String name;

    public PaymentStatus(int id, String name) {
        this.id = id; 
        this.name = name;
    }

    @Override 
    public String toString() {
        return String.format("%s[id: %d, name: %s]",
            getClass().getSimpleName(), id, name);
    }
}

And a List<PaymentStatus> would be preferred over appending integers to any variables.

 public static List<PaymentStatus> getPaymentStatusList() {
    List<PaymentStatus> list = new ArrayList<>();

    paymentStatusList.add(new PaymentStatus(0, "Open"));
    paymentStatusList.add(new PaymentStatus(1, "Approved"));
    paymentStatusList.add(new PaymentStatus(2, "Denied"));

    return list;
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

You're actually concatenating the string "Status_name_" with "0" which would result in "Status_name_0", a string, not a variable like Status_name_0. As far as I understand, you want the values of String_name_i (i= 0, 1, 2,....). To get that working, use String array.

String[] string_names = { "Open", "Approved", "Denied" };

int[] string_id = { 0, 1, 2 };

:You may not need a string_id array, as you can use the values of i in the for loop.

And add them in the list like:

Vars.PaymentStatusName_List.add(string_name[i]);
ram7684
  • 111
  • 1
  • 5
-3
StringBuilder param = new StringBuilder();
param.append("shopid"
        + "=" + shopDetails.getShopId() + "&" + "username" + "=" + userDetail.getUserName() + "&" + "userid" + "=" + userDetail.getUserId() + "&");
for (int i = 0; i < brandList.size(); i++) {
    param.append("brandId" + "[" + i + "]" + "=" + brandList.get(i).getBrandId()
            + "&" + "shortqua" + "[" + i + "]" + "=" + shortageStockList.get(i) + "&");
}
param.append("lattude" + "=" + String.valueOf(latitude) + "&" + "longitude" + "=" + String.valueOf(longitude));
xlm
  • 6,854
  • 14
  • 53
  • 55
Mahadev Dalavi
  • 173
  • 1
  • 7