0

When I compare demDigits.equals(Numbers[i][j]) the if is only true with the last value in the 2d array which is 666, every other value from 111-555 is false. Can't figure out why

public class MainActivity extends AppCompatActivity {

    EditText digits;
    EditText console;
    Button press;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String[][] Numbers = {
                {"111", "222"},
                {"333", "444"},
                {"555", "666"}
        };
        digits = (EditText) findViewById(R.id.editText4);
        console = (EditText) findViewById(R.id.editText5);
        press = (Button) findViewById(R.id.button3);
        press.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String demDigits = digits.getText().toString();

                for (int i = 0; i < Numbers.length; i++)
                {
                    for(int j = 0; j < Numbers[i].length; j++ )
                    {

                        if (demDigits.equals(Numbers[i][j]))
                        {
                            console.setText("works");
                            //break;
                        }
                        else
                        {
                            console.setText("nope");
                        }
                    }
                }
            }
        });
    }
}
magetogi
  • 3
  • 1
  • If you `break` inside that loop you are only breaking out of the inner loop. Not sure if that's what you want, but you can use [labeled loops](http://stackoverflow.com/q/3821827/7366707) to break out of the outer loop. – Salem Jan 22 '17 at 18:32
  • Thats commented, I tried stopping the loop when equals would be true but that didn't help. No need for a break solution. – magetogi Jan 22 '17 at 18:38
  • I am talking about the fact that you aren't stopping the whole loop with that break statement. – Salem Jan 22 '17 at 18:49

1 Answers1

0

You may be getting more than you bargained for from your digits.getText() method as @Ekundayo has pointed out within a comment of the code he posted. Check it, you should get a string length of 3 in order to make a proper comparison:

final String demDigits = digits.getText().toString();

// Read LogCat to see output with method below
// or use whatever you want to display desired 
// output.
Log.d("demDigits", "demDigits Length = " + demDigits.length());

It's never a bad thing to trim User input since additional spaces are easy for a User to apply during that input. For some people it's a downright habit. As @Ekundayo has already suggested:

final String demDigits = digits.getText().toString().trim();

should take care of that issue.

Since you have a commented break statement within your code it is naturally assumed that you would like to break out of your for loops once a successful comparison has been made. This makes total sense since it's a waste of time to process any further unless of course you actually want to. As @1blustone has already pointed out within his comments, to properly break out of both inner and outer for loops you're going to need more than one break statement, you'll need one for each loop. Here is how you can accomplish this:

final String demDigits = digits.getText().toString().trim();

for (int i = 0; i < Numbers.length; i++) {
    boolean mustBreak = false;
    for(int j = 0; j < Numbers[i].length; j++ ) {
        if (demDigits.equals(Numbers[i][j])) {
            console.setText("works");
            mustBreak = true;
            break;
        }
        else {
            console.setText("nope");
        }
    }
    if (mustBreak) { break; }
}

but for some reason, I think you already know this ;)

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22