vendredi 27 juillet 2018

logical error : my program won't enter the if statment [duplicate]

This question already has an answer here:

So I wrote a code to convert standard time to 24hour format but for some reason my program can't enter the if statement.

Code:

public class Solution {
static BufferedReader br;
public static void main(String args[]) throws IOException
{
    br = new BufferedReader( new InputStreamReader(System.in));
    String str = br.readLine();
    String strt = str.substring(8);
    Solution.Time(str,strt); 
}
public static void Time(String str,String Strt) throws IOException
{
    String temp = str.substring(0,8);
    String pureNo[] = temp.split(":");        
    int a[]= new int[3];
    for(int i=0; i<3; i++)
    {
        int Temp = Integer.parseInt(pureNo[i]); 
        a[i] = Temp;
    }
    if(Strt=="AM") 
    {
        if(a[0]==12)
        {
           System.out.println("00"+str.substring(2,8)); 
        }
        else{
            System.out.println(a[0]+str.substring(2,8));
        }
    }
    else
    {
        if(a[0]==12)
        {
            System.out.println("12"+str.substring(2,8));
        }
        else
        {
            System.out.println(a[0]+12+str.substring(2,8));
        }
    }
}

}

so consider the input 01:00:00AM now it gives the ans as 13:00:00, which is wrong. please help.

Aucun commentaire:

Enregistrer un commentaire