jeudi 17 décembre 2015

My if/else statement is always executing else (android)

I am making an app to help me with something (It's quite hard to explain what it is) but it basically takes a 4 letter String from an EditText and outputs another String in the TextView. However the if/else statement is always executing the else even if the if statement is true. I will put my code below.

public class egkk extends ActionBarActivity {
    EditText editText;
    TextView textView;
    Button button;
    String aerodrome;
    String firstTwo;
    String firstThree;
    String first;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_egkk);
    button = (Button)findViewById(R.id.gen);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            editText = (EditText)findViewById(R.id.egkk_edit);
            textView = (TextView)findViewById(R.id.s_view);

            aerodrome = editText.getText().toString().toLowerCase();
            firstTwo = aerodrome.substring(0, 1);
            firstThree = aerodrome.substring(0, 2);
            first = aerodrome.substring(0, 0);

            if (aerodrome.length() == 4){
                if (aerodrome.equals("egkk")){
                    textView.setText("3750 - 3763: Gatwick APC general usage" + "3764 - 3767: Gatwick ADC circuit traffic and transits");
                }
                if (firstThree.equals("egj")){
                    textView.setText("Squawk is 7760 – 7775");
                }
                if (firstTwo.equals("eg")){
                    textView.setText("Sqauwk is 4301 - 4377");
                }
                if (firstTwo.equals("ei")){
                    textView.setText("Squawk is 4430 – 4477");
                }
                if (firstTwo.equals("lf")){
                    textView.setText("Squawk is 2201 – 2277");
                }
                if (firstTwo.equals("le")){
                    textView.setText("Squawk is 2201 – 2277");
                }
                if (firstTwo.equals("lp")){
                    textView.setText("Squawk is 2201 – 2277");
                }
                if (firstTwo.equals("fa")){
                    textView.setText("Squawk is 2201 – 2277");
                }
                if (firstTwo.equals("eh")){
                    textView.setText("Squawk is 7310 – 7677");
                }
                if (firstTwo.equals("eh")){
                    textView.setText("Squawk is 7310 – 7677");
                }
                if (first.equals("k")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (first.equals("c")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (firstTwo.equals("mu")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (firstTwo.equals("my")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (firstTwo.equals("mk")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (firstTwo.equals("mt")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (firstTwo.equals("md")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (firstTwo.equals("tj")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                if (firstTwo.equals("mu")){
                    textView.setText("Squawk is 7610 – 7677");
                }
                else {
                    textView.setText("Squawk is 1140 – 1177");
                }
            }
            else {
                textView.setText("Error");
            }
    }
});



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_egkk, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

} `

Aucun commentaire:

Enregistrer un commentaire