简体   繁体   English

无法解析为变量,可以在哪里声明

[英]cannot be resolved to a variable,where to declare it

I'm getting cant cannot be resolved to a variable ,i know what it's for but i don't know how to fix it.Do i have to declare it somewhere else ? cant cannot be resolved to a variable ,我知道它的用途,但是我不知道如何解决它。我是否必须在其他地方声明它? Where ? 在哪

I have this : 我有这个 :

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

        }

And this : 和这个 :

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();
    }
}

Since you declared cant in calculeaza() , you cannot use it in salveaza() . 既然你宣告cantcalculeaza()你不能用它salveaza() You should declare it outside as an instance variable if you are going to share it between methods. 如果要在方法之间共享它,则应在外部将其声明为实例变量

You can learn more about Java Scope here: Java Programming: 5 - Variable Scope . 您可以在此处了解有关Java范围的更多信息: Java编程:5变量范围

Use ArrayList instead of String[] and declare it as a class field. 使用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();
        } 
    }

 }

There are much better ways of doing this but this addresses your question. 有很多更好的方法可以做到这一点,但这可以解决您的问题。 Use ArrayList since it is much easier than trying to initialise the array at the class level. 使用ArrayList是因为它比尝试在类级别初始化数组要容易得多。

If you want to use the array of Strings you call cant String[] cant = new String[allcant.size()]; 如果要使用字符串数组,则可以调用cant String[] cant = new String[allcant.size()]; in a different method, you can not declare it inside a method. 在不同的方法中,您不能在方法内部声明它。

Declaring a variable inside a method makes it a local method meaning it only exists within that method and can not be seen or used from the outside. 在方法内部声明变量使其成为局部方法,这意味着它仅存在于该方法内,而无法从外部看到或使用。 Your best choice here is to declare it as an instance variable. 您最好的选择是将其声明为实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM