Military Time Problem Exceptions C# | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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
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