简体   繁体   中英

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

Use ArrayList instead of String[] and declare it as a class field.

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.

If you want to use the array of Strings you call 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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