+ 1

Military Time Problem Exceptions C#

Im getting an exception in every case. It says "Input String was not in the correct format" Im using the default code that is automatically written in SL playground. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string twlvhstime; string converted; int thehour; twlvhstime = Console.ReadLine(); if (twlvhstime[6] == 'A') { thehour = Int32.Parse(twlvhstime); Console.WriteLine(thehour); } converted = twlvhstime.Substring(0,5); } } }

31st Dec 2019, 11:10 AM
Fernando Pozzetti
Fernando Pozzetti - avatar
3 Réponses
+ 2
Fernando Pozzetti , you can use Substring method when you parse the string to select the part, which is the hour, otherwise runtime exception is thrown. Please look at the code 🐱 https://code.sololearn.com/co3mShZGup9Y/?ref=app
31st Dec 2019, 12:13 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
TheWh¡teCat 🇧🇬 thank you so much!
31st Dec 2019, 12:15 PM
Fernando Pozzetti
Fernando Pozzetti - avatar
0
You are welcome, Fernando Pozzetti 🐱
31st Dec 2019, 12:16 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar