1

Following is my java code block i am not able to come out of while block, the same code run's perfect in other module. kindly help me

public void current_ER(View v){
        try{ 
                String[] parameters= {uid};
                Caller c=new Caller();

                c.url="current_ER.php";
                c.parameters=parameters;
                c.join(); c.start();

                String result=MainActivity.result;
                System.out.print("before while");
                while(result=="START") {
                    try {
                          Thread.sleep(10); 
                          System.out.print(result); 
                    }
                    catch(Exception ex) { 
                          ex.getLocalizedMessage();
                    System.out.print("in catch"); 
                    }
                }
                System.out.print("after while");    
                Toast.makeText(this, "ER Details->"+result , Toast.LENGTH_LONG).show();
                {                   
                    System.out.print("before start indent block");
                    /////////to next screen////
                    Intent Manage_Expense=new Intent(this,Manage_Expense.class);
                    Manage_Expense.putExtra("er_details", result);
                    //MainActivity.result="START";
                    Toast.makeText(this, "ER Details->"+result , Toast.LENGTH_LONG).show();
                    //startActivity(Manage_Expense);
                }

}catch(Exception e){
                System.out.println(e.getMessage());

}

    };
Max
  • 12,622
  • 16
  • 73
  • 101
Kunal Kor
  • 19
  • 1
  • 9

4 Answers4

4

First of all, use:

while("START".equals(result))

In order to compare Strings.

The main error is that you never update result.

At first you set:

String result=MainActivity.result;

So result and MainActivity.result points to the same object. However, in the other thread you update:

MainActivity.result=resp;

Causing MainActivity.result to point to resp, but result still points to previous value.

If you want to check a variable in a loop, you must make sure the value is changed inside the loop.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1

When you're going to compare 2 strings it's better to use String.equals(), == operator better works on primitives but not on objects.

Ulaga
  • 863
  • 1
  • 8
  • 17
0

As well as using String#equals() make sure the variable is marked as volatile so threads can see changes made by other threads.

Tim B
  • 40,716
  • 16
  • 83
  • 128
0
i updated my code as follows and it worked 

 System.out.print("before while");
               while("START".equals(MainActivity.result)) {
                    try {
                        Thread.sleep(10); 
                        System.out.print(MainActivity.result); 
                    }catch(Exception ex) { ex.getLocalizedMessage();
                    System.out.print("in catch"); 

                    }
                }

but i am still unable to figure out why my original code didn't work

Kunal Kor
  • 19
  • 1
  • 9