试试我的例子,根据你的 ID 格式JP100001
,ID 的长度为8,前缀为JP和JU。
/**
* This method is used to format plain id (plain number). Mostly,
* this method is used in setId() methods of entities.
* <p>
* E.g.
* Input: id=523, prefix="JP", maxLength=15
* Output: JP0000000000523
* <p>
* @param id id must not be null.
* @param prefix prefix must not be null.
* @param maxLength maximum length of the id string and it must not be negative number.
* @return a formatted id string
*/
public static String formatId(String id, String prefix, int maxLength) {
if (!id.startsWith(prefix)) {
int length = id.length() + prefix.length();
for (; (maxLength - length) > 0; length++) {
id = '0' + id;
}
id = prefix + id;
}
return id;
}
yourMethod() {
try {
int div = view.getCbDiv().getSelectedIndex();
int l = 100000;
do {
if ( div == 0 ) {
view.getTxtJobCode().setText(formatId((l + ""), "JP, 8));
}
else {
view.getTxtJobCode().setText(formatId((l + ""), "JU, 8));
}
} while(l <= 199999);
l++;
}
}