Java function calling and return value in Android

temp = temp + Character.toString((char)c);

You don't define temp in the read() method, so it is probably defined as a global variable. This means that every time you call the read() method, you are appending the new values to it. You should probably define temp in your read() method:

String temp;

that should fix it.


if(flag==1)
    {
        Log.d("Flag value", "flag= "+flag);
        //System.out.println("Read have "+read());
        String tt=read();
        s1=tt;
    }

in above code read() method is calls twice. And inside read() method variable "temp" is declared global and you are concat the data like

temp = temp + Character.toString((char)c);

so value is concat twice in temp variable.

To Resolve the issue declare temp as local variable like

public String read(){
          String temp="";
          try{
             FileInputStream fin = openFileInput(file);
             int c;

             while( (c = fin.read()) != -1)
             {
                temp = temp + Character.toString((char)c);
             }
          }
          catch(Exception e)
          {

          }
          Log.d("INSIDE READ FUNC", "temp have "+temp);
        return temp;
       }