1

How can I check the below conditions in switch instead of if?

I want to check this conditions in switch case.

if(tvStartLocation.getText().toString().equalsIgnoreCase(""))
{
    Toast.makeText(getActivity(),"Please enter start location", Toast.LENGTH_SHORT).show();
}
else if(tvEndLocation.getText().toString().equalsIgnoreCase(""))
{
    Toast.makeText(getActivity(),"Please enter end location", Toast.LENGTH_SHORT).show();
}
else if(etStartOdometer.getText().toString().equalsIgnoreCase(""))
{
    Toast.makeText(getActivity(),"Please enter Trip Start Odometer reading", Toast.LENGTH_SHORT).show();
}
else if(etEndOdometer.getText().toString().equalsIgnoreCase(""))
{
    Toast.makeText(getActivity(),"Please enter Trip End Odometer reading", Toast.LENGTH_SHORT).show();
}
else if((compareOdometerReading(etStartOdometer.getText().toString(),etEndOdometer.getText().toString())) == -1)
{
    Toast.makeText(getActivity(),"End Odometer reading should be greater than Start Odometer Reading (eOR > sOR)!", Toast.LENGTH_SHORT).show();
}
else if(etManifest.getText().toString().equalsIgnoreCase(""))
{
    Toast.makeText(getActivity(),"Please enter Manifest", Toast.LENGTH_SHORT).show();
}
else if(etShipper.getText().toString().equalsIgnoreCase(""))
{
    Toast.makeText(getActivity(),"Please enter Shipper", Toast.LENGTH_SHORT).show();
}
else if(etCommodity.getText().toString().equalsIgnoreCase(""))
{
    Toast.makeText(getActivity(),"Please enter Commodity", Toast.LENGTH_SHORT).show();
}

Suggest Me Guys In Advance

Manikanta Reddy
  • 631
  • 9
  • 15
  • You can't. You are comparing a bunch of different variables to one constant. Switches are for comparing one variable to a bunch of constants. You could, however simplify your `if` conditions to `...getText().isEmpty()`, assuming `getText()` returns a `String` anyway. – khelwood Feb 09 '15 at 11:43
  • You need Java 1.7 for that! Still your use case will not allow to use the same! TO reduce your if else chain I recommend that you go through [this](http://stackoverflow.com/questions/7349883/how-to-remove-large-if-else-if-chain) – Skynet Feb 09 '15 at 11:44
  • You are not comparing the same string. – Renan Gomes Feb 09 '15 at 11:44

3 Answers3

2

In a switch statement you compare one variable against different constant values. You want to compare different variables against one constant value. This is not possible, and IMHO, does not make sense in your case.

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29
0

As others mentioned you are comparing multiple variables to a constant, and switch cannot do that. But you could improve code reuse and readability by putting all those variables in array or list and then iterating over them. For example something like this:

TextView[] toValidate = new TextView[]{tvStartLocation, tvEndLocation, etStartOdometer}; //etc

for(TextView tv : toValidate){
     if(tv.getText().toString().equalsIgnoreCase(""))
     {
          //Display message.. 
     }
}
Ivan
  • 1,735
  • 1
  • 17
  • 26
0

You can't replace them with a switch statement, since each condition checks a different variable. You could replace them with a method, to avoid code duplication.

public boolean testControl (EditText control, String name) { // I'm assuming your control is an EditText 
                                              // (perhaps I got the type wrong)
    if (control.getText().toString().equalsIgnoreCase("")) {
        Toast.makeText(getActivity(),"Please enter " + name, Toast.LENGTH_SHORT).show();
        return false;
    }
    return true;
}

You can call the method :

boolean hasValue = testControl (etShipper, "Shipper");

Then, if you store your EditText controls in some array/Collection, you can call this method in a loop.

Eran
  • 387,369
  • 54
  • 702
  • 768