0

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.

Jon
  • 113
  • 1
  • 11
  • 2
    `pkCheck` is always 0. `val++;` won't effect that since the `if` is already executed, the `else` won't be executed. – Maroun Jun 26 '13 at 05:19
  • thanks everyone. I actually missed something so simple. – Jon Jun 26 '13 at 06:18

3 Answers3

3

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());*

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
2

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

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 1
    think OP's intent _is_ sending zero, but thinking val++ will change the outer value pkCheck – epoch Jun 26 '13 at 05:22
  • @epoch .. you are passing the parameter as value. so it will change the val for local scope.. but pcheckes value will not be changed – stinepike Jun 26 '13 at 05:26
2

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

i.e. executing val++ will not change pkCheck


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

Community
  • 1
  • 1
epoch
  • 16,396
  • 4
  • 43
  • 71