I have the following code snippet where some strings are initialized in the if block:
String serialmask = request.getParameter( "serialmask");
String serialincrement = request.getParameter( "serialincrement");
if (serialmask == "1") {
String tserialmask = "aaa########";
}
else {
String tserialmask = "";
}
if (serialincrement == "1") {
String tserialincrement = "aaa^^^^^^^^";
}
else {
String tserialincrement = "";
}
out.println(
itemimport(
partnumber,
itemcost,
itemlistprice,
itemdescription,
PurchProdLineKey,
UnitMeasKey,
itemclasskey,
trackmethod,
tserialmask,
tserialincrement
)
);
The error I’m getting is "cannot find symbol" in the
symbol : variable tserialmaskout.println(itemimport(....tserialmask,tserialincrement)); statement.
I tried declaring the variables outside of the if block and this seems to bring on even more errors saying it’s already been declared.
You need to declare the variable first, but then just assign it. Here’s the version for
tserialincrement(the same is true fortserialmask)However, there are two things wrong with this:
equalsYou can do it in one statement (per variable) with the conditional operator:
In addition, I’d suggest nicer variable names, using Pascal casing (e.g.
serialMask) and something more useful than just “t” as a prefix. (What does that mean?)