Answered by:
Input string was not in a correct format

Question
-
I gained this lovely error of "Input string was not in a correct format" from the following code, any help please?
namespace Throne.World.Database.Client.Files { /// <summary> /// Serial number for each item type. Also used for the auction interface. /// </summary> public static class ClassDesc { public const String Path = "system/database/ClassDesc.ini"; public static void Read(out Dictionary<String, Int32> dict) { dict = new Dictionary<String, Int32>(); using (var reader = new FileReader(Path)) foreach (string line in reader) { string[] data = line.Split('#'); int num = Convert.ToInt32(data[0]); <<<----Error is on this line dict[data[1]] = num < 1000 ? num * 1000 : num; } } } }
- Edited by WHITELIONX Tuesday, February 3, 2015 4:19 PM Adding the error line
Answers
-
Then probably split on ' ' (a space character) rather than a '#' (octothorpe).
- Marked as answer by WHITELIONX Tuesday, February 3, 2015 5:27 PM
All replies
-
try this:
int num = Int.Parse(data[0]);
- Proposed as answer by Bouzid Mohamed Wednesday, February 4, 2015 2:56 PM
-
-
This simply means that the text in the file cannot be interpreted as an integer.
You've split your line up based on the # delimiter and taken the first field.
So based on that alone, I might expect some text like this in the file
123#test
That would actually work just fine. Because it would split the text to get a string array: { "123", "test" } and would parse the first one "123" to get the integer 123.
But based on the extension of the filename that you used in your example (.ini), an ini file is unlikely to have every line in a suitable format. In fact, I would expect an .ini file to look more like this:
; last modified 1 April 2001 by John Doe [owner] name=John Doe organization=Acme Widgets Inc.
Your code would parse this by reading the first line, splitting it on # (which doesn't exist) and taking the entire text of the first line "; last modified 1 April 2001 by John Doe" and trying to parse it as an integer. Which will throw a System.FormatException, claiming that the "Input string was not in a correct format."
To allow for lines that don't contain integer data, you can use int.TryParse, which will return false if it fails to parse the text as an integer. Then you can handle this case by skipping the line, or by doing whatever is most appropriate for your situation.
- Edited by Wyck Tuesday, February 3, 2015 5:05 PM minor typo
-
Yes the .ini file in question is laid out like so
1 Weapon 2 Headgear 3 Armor 4 Ring/Bracelet 5 Necklace/Bag 6 Boots 7 Talisman 8 Mount 9 Garment/Accessory 10 Gem 11 Valuables 12 Dragon/Martial~Soul 13 Refinery~Material 14 Others 1000 Blade 1010 Sword 1020 Club 1030 Prayer~Beads 1040 Ninja~Katana 1050 Backsword 1060 Rapier 1070 Pistol 1080 Bow 1090 Shield 1100 Wand 1110 Spear 1120 Dagger 1130 Hook 1140 Axe 1150 Hammer 1160 Scepter 1170 Whip 1180 Glaive 1190 Halbert 1200 Long~Hammer 1210 Poleaxe 1220 Arrow 1230 Pickaxe 1240 Weapon~Accessory 1250 Scythe 1260 ThrowingKnife 1270 Cross~Saber 1280 Nobunaga`sClaw 1290 Nunchaku 2000 Earring 2010 Trojan~Coronet 2020 Warrior~Helmet 2030 Warrior~Headband 2040 Ninja~Hood 2050 Ninja~Veil 2060 Monk~Headband 2070 PirateHat 2080 Taoist~Cap 2090 Archer~Hat 2100 Archer~Plume 2110 Other 2111 Dragon~warriorHeadband 2120 PirateHood 3000 Trojan~Armor 3010 Warrior~Armor 3020 Ninja~Vest 3030 Monk~Frock 3040 PirateCoat 3050 Taoist~Robe 3060 Archer~Coat 3070 Other 3080 Dragon~warriorSuit 4000 Ring 4010 Heavy~Ring 4020 Bracelet 5000 Necklace 5010 Bag 7000 Heaven~Fan 7010 Star~Tower 7020 Riding~Crop 7030 Other 9000 Garment 9010 Mount~Armor 9020 2-handed~Accessory 9030 1-handed~Accessory 9040 Bow~Accessory 9050 Shield~Accessory 10000 Thunder~Gem 10010 Glory~Gem 10020 Tortoise~Gem 10030 Dragon~Gem 10040 Phoenix~Gem 10050 Rainbow~Gem 10060 Violet~Gem 10070 Moon~Gem 10080 Fury~Gem 10090 Kylin~Gem 11000 Dragon~Ball 11010 Permanent~Stone 11020 Stone 11030 Tough~Drill 11040 Star~Drill 11050 Praying~Stone 11060 Exemption~Token 11070 Meteor 11080 Meteor~Tear 11090 EXP~Ball 12000 For~Weapon 12010 For~Headgear 12020 For~Necklace 12030 For~Armor 12040 For~Ring 12050 For~Boots 13000 Intensification 13010 Detoxication 13020 Critical~Strike 13030 Skill~Critical~Strike 13040 Penetration 13050 Strike~Resistance 13060 Block 13070 Breakthrough 13080 Counteraction 13090 Magic~Defense 13100 Resistance 14000 Quest~Item 14010 Skill~Book 14020 Potion 14030 Pack 14040 Other
-
Then probably split on ' ' (a space character) rather than a '#' (octothorpe).
- Marked as answer by WHITELIONX Tuesday, February 3, 2015 5:27 PM
-