Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的if/else语句总是执行else(android)_Java_Android_If Statement_Android Edittext - Fatal编程技术网

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

Java 我的if/else语句总是执行else(android),java,android,if-statement,android-edittext,Java,Android,If Statement,Android Edittext,我正在制作一个应用程序来帮助我做一些事情(很难解释它是什么),但它基本上从EditText中获取一个4个字母的字符串,并在TextView中输出另一个字符串。但是,if/else语句始终在执行else,即使if语句为true也是如此。我会把我的代码放在下面 public class egkk extends ActionBarActivity { EditText editText; TextView textView; Button button

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

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

`这是因为您的病情:

if (aerodrome.length() == 4){
从不匹配。这意味着,在这里,当你得到文本时,它永远不是一个长度为4的字符串

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

对不起,我的意思是嘎嘎1140-1177总是在执行,但我发现了原因。为了解决这个问题,我将第一个if之后的所有if都改为else if,并且在子字符串上,我错误地少了一个索引,所以不是

前两个=机场。子串(0,1)

我把它改成了

First2=机场。子串(0,2)


尽管如此,还是感谢您的帮助。

对不起,这是显而易见的,问题的作者问,为什么不这样做match@GabriellaAngelova原谅。但更明显的是,为什么它不匹配。。。。不是吗
editText
不包含精确的4个字符…确切地说,需要输入,而且很明显检查不满足,这就是为什么它会移动到Elsery来调试您的代码,并在您从TextViewtry分配值到using Airporome.length()>=4或Airporome.length()后查看Airporome有什么值@RavindraPawar不需要这样做,如果else子句总是执行
if(airprome.length()==4){
永远都不是真的……仅此而已