简体   繁体   中英

Does StringBuilder not append whitespaces?

I am working on a web application using Woodstock component. In short, if the dropdown value equals to "PRIMARY KEY", the function should store the name of the table (from the textfield) into the String pk/primaryKey.

The problem I am facing currently is that the results obtained from the function is:

"abc"

instead of

"a, b, c"

Why is this happening?

public void addPK(TextField tf, DropDown dd, StringBuilder pk, int val) {
    if (((String) dd.getValue()).equals("PRIMARY KEY")) {
        if (val == 0) {
            pk.append((String) tf.getValue());
            val++;
        } else {
            pk.append(", " + (String) tf.getValue());
        }
    }
}


public String createButton1_action() {
    StringBuilder primaryKey = new StringBuilder();
    int pkCheck = 0;

    addPK(column_TF1, pkDropDown1, primaryKey, pkCheck);
    addPK(column_TF2, pkDropDown2, primaryKey, pkCheck);
    addPK(column_TF3, pkDropDown3, primaryKey, pkCheck);
    .
    .
}

Thank you.

pkcheck value is always zero when you calling your method 'addPK'. S for that either increment pkcheck value after every call or you can cheak with the value of your stringbuider length you can change your method like

Option 1.

public void addPK(TextField tf, DropDown dd, StringBuilder pk) {
    if (((String) dd.getValue()).equals("PRIMARY KEY")) {
        if (pk.length()== 0) {
            pk.append((String) tf.getValue());
        } else {
            pk.append(", ").append((String) tf.getValue());
        }
    }
}


Option 2.

public int addPK(TextField tf, DropDown dd, StringBuilder pk, int val) {
    if (((String) dd.getValue()).equals("PRIMARY KEY")) {
        if (val == 0) {
            pk.append((String) tf.getValue());
            val++;
        } else {
            pk.append(", " + (String) tf.getValue());
        }
    }
    return val;
}


public String createButton1_action() {
    StringBuilder primaryKey = new StringBuilder();
    int pkCheck = 0;

    pkCheck = addPK(column_TF1, pkDropDown1, primaryKey, pkCheck);
    pkCheck = addPK(column_TF2, pkDropDown2, primaryKey, pkCheck);
    pkCheck = addPK(column_TF3, pkDropDown3, primaryKey, pkCheck);
    .
    .
}

*And if your are using StringBuilder or StringBuffer the best practice is to use append operation instead of '+' operator

pk.append(", " + (String) tf.getValue());

replace with
pk.append(", ").append((String) tf.getValue()); *

Because you are sending a val parameter with the value 0

  int pkCheck = 0;   

    addPK(column_TF1, pkDropDown1, primaryKey, pkCheck); // value is 0 here

and in addpK method

if (val == 0) {
    pk.append((String) tf.getValue());
    val++;
}

So to get the desired result you need to set the value of pCheck to other values rather than 0

have you debugged? val stays at 0 the whole time. modifying it within your method has no effect.

ie executing val++ will not change pkCheck


for reference: java-is-pass-by-value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM