我的if / else语句总是执行else(android)

时间:2015-12-17 12:37:55

标签: java android if-statement android-edittext

我正在制作一个应用程序来帮助我(很难解释它是什么)但它基本上从EditText中获取一个4个字母的字符串,并在TextView中输出另一个字符串。但是,即使if语句为true,if / else语句也始终执行else。我将把我的代码放在下面。

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);
    }
}

`

2 个答案:

答案 0 :(得分:2)

这是因为你的条件:

if (aerodrome.length() == 4){

绝不匹配。这意味着,在这里,当你得到文本时,永远不会是4 length的字符串。

aerodrome = editText.getText().toString().toLowerCase();

答案 1 :(得分:0)

对不起,我的意思是Squawk 1140 - 1177一直在执行,但我发现了原因。为了解决这个问题,我将第一个ifs之后的所有ifs更改为else,如果在子字符串上我错误地少了一个索引而不是

firstTwo = aerodrome.substring(0,1);

我把它改成了

firstTwo = aerodrome.substring(0,2);

感谢您的所有帮助。