Hi please I'm converting 12-hour AM/PM format, to military 24-hour time format using C# Console app.
Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
My code is that :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
/*
* Complete the timeConversion function below.
*/
static string timeConversion(string s) {
/*
* Write your code here.
*/
String result = string.Empty;
String strconv = s.Substring(0, 2);
int convt = Int32.Parse(strconv);
if (s.Substring(8).equals("AM")) {
if (s.Substring(0, 2).equals("12")) {
result = s.Substring(0, 8);
}
} else if (s.Substring(8).equals("PM")) {
if (s.Substring(0, 2).equals("24") || convt == 12) {
result = "00" + s.Substring(0, 2);
} else if (convt < 12) {
convt = convt + 12;
result = convt + s.Substring(2, 8);
}
}
return result;
}
static void Main(string[] args) {
TextWriter tw = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string s = Console.ReadLine();
string result = timeConversion(s);
tw.WriteLine(result);
tw.Flush();
tw.Close();
}
}
but When I am executing the above code getting error as below
solution.cs(18,28): error CS1061: Type `string' does not contain a definition for `equals' and no extension method `equals' of type `string' could be found. Are you missing an assembly reference?
Please, How can I fix that maybe I have missing an assembly reference but don't know how to resolve this.
You are not missing any namespace in your C# code, you are already using System for .Equals, "E" just need to be in capital letter
Here is the code which worked for me when executed in my local Visual studio
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace TimeConversion
{
class Program
{
/*
* Complete the timeConversion function below.
*/
static string timeConversion(string s)
{
/*
* Write your code here.
*/
String result = string.Empty;
String strconv = s.Substring(0, 2);
int convt = Int32.Parse(strconv);
if (s.Substring(8).Equals("AM"))
{
if (s.Substring(0, 2).Equals("12"))
{
result = s.Substring(0, 8);
}
}
else if (s.Substring(8).Equals("PM"))
{
if (s.Substring(0, 2).Equals("24") || convt == 12)
{
result = "00" + s.Substring(0, 2);
}
else if (convt < 12)
{
convt = convt + 12;
result = convt + s.Substring(2, 8);
}
}
return result;
}
static void Main(string[] args)
{
TextWriter tw = new StreamWriter(@"E:\Outputtime.txt", true);
string s = Console.ReadLine();
string result = timeConversion(s);
tw.WriteLine(result);
tw.Flush();
tw.Close();
}
}
}
Here is the screenshot which shows Code/Output
As you can see in the above code you need to replace ".equals()" to ".Equals()".
Thank you so much pika :) .
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly