Reputation: 113
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.
Upvotes: 0
Views: 2053
Reputation: 3026
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
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());
}
}
}
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());
*
Upvotes: 3
Reputation: 16615
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
Upvotes: 2
Reputation: 54692
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
Upvotes: 2