1

I am making an android app for easier time encoding, I programmed it so that whenever I put numbers it outputs time in A.M. and if I put a "p" at the end of those numbers it adds a P.M. at the end.

sat_amin.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            EditText sat_amin = (EditText) findViewById(R.id.asd);
            TextView dsa = (TextView) findViewById(R.id.dsa);
            String satamin = sat_amin.getText().toString();
            int satamln = satamin.length();
            String last = satamin.substring(satamin.length() - 1);
            String p = "p";

            switch(satamln) {
                case 1:
                    sat_amin.setText("12:0" + satamin + " AM");
                    break;

                case 2:
                    if (last == p) {
                        sat_amin.setText("12:0" + satamin + " PM");
                    } else {
                        sat_amin.setText("12:" + satamin + " AM");
                    }
                    break;

                case 3:
                    if (last == p) {
                        sat_amin.setText("12:" + satamin + " PM");
                    } else {
                        sat_amin.setText("0" + satamin.substring(0, 1) + ":" + satamin.substring(1) + " AM");
                    }
                    break;

                case 4:
                    if (last == p) {
                        sat_amin.setText("0" + satamin.substring(0, 1) + ":" + satamin.substring(1, 3) + " PM");
                    } else {
                        sat_amin.setText(satamin.substring(0, 2) + ":" + satamin.substring(2) + " AM");
                    }
                    break;

                case 5:
                    if (last == p) {
                        sat_amin.setText(satamin.substring(0, 2) + ":" + satamin.substring(2, 4) + " PM");
                    } else {
                        sat_amin.setText(satamin.substring(0, 1) + ":" + satamin.substring(1, 3) + ":" + satamin.substring(3, 5) + " AM");
                    }
                    break;

                default:
                    sat_amin.setText("");
                    break;
            }



            return false;
        }
    });

When I run the app the AM part of the code works just fine, but when I add a "p" it doesn't get compared in the if statement and the "p" gets outputted as if the whole string didn't have a "p" in the end.

lathrix
  • 115
  • 1
  • 6
  • Welcome to Stackoverflow. Please try to create a [mcve](https://stackoverflow.com/help/mcve). That makes it easier to answer the question. – leonardkraemer Apr 18 '18 at 23:08

1 Answers1

1

It looks like you want to know if last and p are equal, not if they are the same object (have the same reference):

if (last == p) {
    sat_amin.setText("12:0" + satamin + " PM");
} else {
    sat_amin.setText("12:" + satamin + " AM");
}

In that case you should use equals instead of ==.

if (last.equals(p)) {
    sat_amin.setText("12:0" + satamin + " PM");
} else {
    sat_amin.setText("12:" + satamin + " AM");
}
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46