Java 无法解析为变量,在何处声明它
我无法将Java 无法解析为变量,在何处声明它,java,android,Java,Android,我无法将解析为变量,我知道它的用途,但不知道如何修复它。我必须在其他地方声明它吗?在哪里 我有这个: public void calculeaza() { totaltest = 0; String[] cant = new String[allcant.size()]; for (int j = 0; j < allcant.size(); j++) { cant[j] = allcant.get(j).getText().toString(
解析为变量,我知道它的用途,但不知道如何修复它。我必须在其他地方声明它吗?在哪里
我有这个:
public void calculeaza() {
totaltest = 0;
String[] cant = new String[allcant.size()];
for (int j = 0; j < allcant.size(); j++) {
cant[j] = allcant.get(j).getText().toString();
if (cant[j].matches("")) {
Toast.makeText(this,
"Ati omis cantitatea de pe pozitia " + (j + 1),
Toast.LENGTH_SHORT).show();
cant[j] = Float.toString(0);
}
由于您在calculeaza()
中声明了cant
,因此无法在salveaza()
中使用它。如果要在方法之间共享它,则应该在外部将其声明为
您可以在此处了解有关Java作用域的更多信息: 使用ArrayList而不是String[],并将其声明为类字段
public class MyClass{
private ArrayList<String> cant; // <---- accessible by all methods in the class.
public void calculeaza() {
cant = new ArrayList<String>();
for (int j = 0; j < allcant.size(); j++) {
cant.add(allcant.get(j).getText().toString());
if (cant.get(j).matches("")) {
Toast.makeText(this,
"Ati omis cantitatea de pe pozitia " + (j + 1),
Toast.LENGTH_SHORT).show();
cant.get(j) = Float.toString(0);
}
....
public void salveaza(){
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(cant[1]);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}
公共类MyClass{
private ArrayList cant;//如果要在其他方法中使用调用的字符串数组cantString[]cant=new String[allcant.size()];
,则不能在方法中声明它
在一个方法中声明一个变量会使它成为一个局部方法,这意味着它只存在于该方法中,从外部看不到或无法使用。您最好的选择是将它声明为一个实例变量。@Nambari这将很难,因为代码无法编译!哪一行显示了错误?@Simon:IDE可能是什么意思显示错误,我们需要行号详细信息等。问题是无法[]是在public void calculeaza中声明的,我正试图在public void salveaza中使用它,对吗?我必须在范围之外声明它?你能给我一个简单的例子说明我怎么做吗?谢谢!@SorinGrecu这就是为什么他发布了一个链接。在你的类内部,但在你的方法外部,键入private String[]cant=new String[allcant.size()];
。您需要确保allcant
也是一个实例变量。我现在得到赋值的左侧必须是一个变量atcant.get(j)=Float.toString(0);
public class MyClass{
private ArrayList<String> cant; // <---- accessible by all methods in the class.
public void calculeaza() {
cant = new ArrayList<String>();
for (int j = 0; j < allcant.size(); j++) {
cant.add(allcant.get(j).getText().toString());
if (cant.get(j).matches("")) {
Toast.makeText(this,
"Ati omis cantitatea de pe pozitia " + (j + 1),
Toast.LENGTH_SHORT).show();
cant.get(j) = Float.toString(0);
}
....
public void salveaza(){
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(cant[1]);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}