Java错误:找不到符号?

时间:2022-01-10 20:50:09

I have the following code snippet where some strings are initialized in the if block:

我有以下代码片段,其中一些字符串在if块中初始化:

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" symbol : variable tserialmask in the out.println(itemimport(....tserialmask,tserialincrement)); statement.

我得到的错误是“找不到符号”符号:out.println中的变量tserialmask(itemimport(.... tserialmask,tserialincrement));声明。

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.

我尝试在if块之外声明变量,这似乎带来了更多的错误,说它已经被声明了。

4 个解决方案

#1


You need to declare the variable first, but then just assign it. Here's the version for tserialincrement (the same is true for tserialmask)

您需要先声明变量,然后再分配它。这是tserialincrement的版本(对于tserialmask也是如此)

String tserialincrement;
if (serialincrement == "1")
{
   tserialincrement = "aaa^^^^^^^^";
}
else
{ 
   tserialincrement = "";
}

However, there are two things wrong with this:

但是,这有两个问题:

  • You're using == on a string, which is a bad idea in almost all situations; use equals
  • 你在字符串上使用==,这在几乎所有情况下都是一个坏主意;使用等于

  • You can do it in one statement (per variable) with the conditional operator:

    您可以使用条件运算符在一个语句(每个变量)中执行此操作:

    String tserialmask = "1".equals(serialmask) ? "aaa########" : "";
    String tserialincrement = "1".equals(serialincrement) ? "aaa^^^^^^^^" : "";
    

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?)

另外,我建议使用更好的变量名,使用Pascal大小写(例如serialMask)以及比“t”作为前缀更有用的东西。 (那是什么意思?)

#2


You need to declare tserialmask and tserialincrement outside of the if/else blocks. Otherwise, they go out of scope when that block ends.

您需要在if / else块之外声明tserialmask和tserialincrement。否则,当该块结束时,它们将超出范围。

String serialmask = request.getParameter( "serialmask");
String serialincrement = request.getParameter( "serialincrement");
String tserialmask;
String tserialincrement;

if (serialmask == "1")
{  
  tserialmask = "aaa########";
}
else
{ 
  tserialmask = "";
}
if (serialincrement == "1")
{
  tserialincrement = "aaa^^^^^^^^";
}
else
{ 
  tserialincrement = "";
}
out.println(itemimport(partnumber,itemcost,itemlistprice,itemdescription,PurchProdLineKey,UnitMeasKey,itemclasskey,trackmethod,tserialmask,tserialincrement));

#3


You are declaring String variables within the if else statements then trying to access them outside the statements. You need to declare the variables before your if statements and then only assign them within the if/else statements.

您在if else语句中声明String变量,然后尝试在语句之外访问它们。您需要在if语句之前声明变量,然后仅在if / else语句中指定它们。

#4


You need to declare the variables tserialmask and tserialincrement outside the if branches and not try to redeclare them inside, like this:

你需要在if分支之外声明变量tserialmask和tserialincrement,而不是尝试在里面重新声明它们,如下所示:

String tserialmask;
if (serialmask == "1") {  
    tserialmask = "aaa########"; 
} else { 
    tserialmask = ""; 
} 

#1


You need to declare the variable first, but then just assign it. Here's the version for tserialincrement (the same is true for tserialmask)

您需要先声明变量,然后再分配它。这是tserialincrement的版本(对于tserialmask也是如此)

String tserialincrement;
if (serialincrement == "1")
{
   tserialincrement = "aaa^^^^^^^^";
}
else
{ 
   tserialincrement = "";
}

However, there are two things wrong with this:

但是,这有两个问题:

  • You're using == on a string, which is a bad idea in almost all situations; use equals
  • 你在字符串上使用==,这在几乎所有情况下都是一个坏主意;使用等于

  • You can do it in one statement (per variable) with the conditional operator:

    您可以使用条件运算符在一个语句(每个变量)中执行此操作:

    String tserialmask = "1".equals(serialmask) ? "aaa########" : "";
    String tserialincrement = "1".equals(serialincrement) ? "aaa^^^^^^^^" : "";
    

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?)

另外,我建议使用更好的变量名,使用Pascal大小写(例如serialMask)以及比“t”作为前缀更有用的东西。 (那是什么意思?)

#2


You need to declare tserialmask and tserialincrement outside of the if/else blocks. Otherwise, they go out of scope when that block ends.

您需要在if / else块之外声明tserialmask和tserialincrement。否则,当该块结束时,它们将超出范围。

String serialmask = request.getParameter( "serialmask");
String serialincrement = request.getParameter( "serialincrement");
String tserialmask;
String tserialincrement;

if (serialmask == "1")
{  
  tserialmask = "aaa########";
}
else
{ 
  tserialmask = "";
}
if (serialincrement == "1")
{
  tserialincrement = "aaa^^^^^^^^";
}
else
{ 
  tserialincrement = "";
}
out.println(itemimport(partnumber,itemcost,itemlistprice,itemdescription,PurchProdLineKey,UnitMeasKey,itemclasskey,trackmethod,tserialmask,tserialincrement));

#3


You are declaring String variables within the if else statements then trying to access them outside the statements. You need to declare the variables before your if statements and then only assign them within the if/else statements.

您在if else语句中声明String变量,然后尝试在语句之外访问它们。您需要在if语句之前声明变量,然后仅在if / else语句中指定它们。

#4


You need to declare the variables tserialmask and tserialincrement outside the if branches and not try to redeclare them inside, like this:

你需要在if分支之外声明变量tserialmask和tserialincrement,而不是尝试在里面重新声明它们,如下所示:

String tserialmask;
if (serialmask == "1") {  
    tserialmask = "aaa########"; 
} else { 
    tserialmask = ""; 
}