Question

During compiling my new Android application Android Studio exit whit this error: "Cannot find symbol variable percscon".

My code is

if(ulterioretext.equals("")) {
            ....
        } else {

            String uno =  (String) "1";
            float numbersconto = Float.valueOf(ulterioretext);

            if(numbersconto <= 9){
                String percscont = (String) uno + ".0" + ulterioretext;
            } else {
                String percscont = (String) uno + "." + ulterioretext;
            }

            float percscontfloat = Float.valueOf(percscont); <- The error is here

            ....
        }

If i put the "String percscont = (String) uno + ".0" + ulterioretext;" outside the if everything works fine, but if It is inside the "if" The compiler did not find the percscont variable. Why?

Thanks

Was it helpful?

Solution

Cannot find symbol variable '''percscon'''.

And you have used variable

 float percscontfloat = Float.valueOf(percscont); 

It must be typo. No doubt

so as you have declared variable which has scope inside if and else block.

 if(numbersconto <= 9){
                String percscont = (String) uno + ".0" + ulterioretext;
            } else {
                String percscont = (String) uno + "." + ulterioretext;
            }

Outside this variable is not accessible.

So declare it outside or start of method or block and make it accessible after if-else.

OTHER TIPS

I solved the problem myself.

I had to initialize the variable outside the if and now works fine!

Thanks

If you declaring percscont variable in if scope then you cannot access it from outside, if you want to access outside from if then declare in an upper scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top