0

Consider a String variable pri with value 07:45:32PM , now in order to obtain the PM alone I applied,

pri = pri.replaceAll("[^A-Z]","");

So far things work fine, but trying to compare the value in the variable does not work, ie :

   if(pri=="PM")
   {
        hh+=12;

   }

The body of the loop does not get executed. My question is are the two values different, ie Pri=="PM" , Why is it so? And how do I get to check my if loop in a precise way? Thank you

EDIT1

So I tried if(pri.equals("PM")) instead of if(pri=="PM") , but still it did not solve the problem!

OBX
  • 6,044
  • 7
  • 33
  • 77
  • 2
    use pri.equals("PM") – Mikey Nov 25 '15 at 16:00
  • Your code with that input works as expected for me when using `equals`. You must be doing something different which you haven't shown. – resueman Nov 25 '15 at 16:07
  • 1
    For your actual problem, did you consider using something like [`new SimpleDateFormat("KK:mm:ssaa").parse(pri)`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)? – JimmyB Nov 25 '15 at 16:10
  • Show your complete code (getting date string, replaceAll, comparing). Dump the value of pri, it might not be "PM" as you expect. – Werner Henze Nov 25 '15 at 16:32

3 Answers3

1

EDIT: Use "equalsIgnoreCase()" method for comparing strings.

   if(pri.equalsIgnoreCase("PM"))
  {
    hh+=12;

   }
Perdomoff
  • 938
  • 2
  • 7
  • 26
1

You need to use .equals not == to compare two String Object.

    if(pri.equals("PM"))
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
rainkinz
  • 10,082
  • 5
  • 45
  • 73
0

Use if(pri.equals("PM")) instead.

b han
  • 28
  • 5