-2

I found a problem - when I run this program, Visual Studio throws an exception - Format exception: Input string was not in a correct format.

        string name = Console.ReadLine();
        int age = int.Parse(Console.ReadLine());
        int id = int.Parse(Console.ReadLine());
        double salary = double.Parse(Console.ReadLine());
        Console.WriteLine($"Name: {name}");
        Console.WriteLine($"Age: {age}");
        Console.WriteLine($"Employee ID: {id:D8}");
        Console.WriteLine($"Salary: {salary:F2}");

Unfortunately, I can't find a mistake here. Can you help me, please? Or this is a bug in Visual Studio?

  • You are trying to convert what is in the Colsole.ReadLine to an int, but do you know if it is an int before you try to convert it? It could be somethign else – Brad Apr 13 '18 at 14:50
  • Exception has been thrown by one of `.Parse(Console.ReadLine())`: to reproduce just put `bla-bla-bla` when `int` or `double` required – Dmitry Bychenko Apr 13 '18 at 14:51
  • Can you share your input? – Long Luong Apr 13 '18 at 14:54
  • Use [`int.TryParse`](https://learn.microsoft.com/en-ca/dotnet/api/system.int32.tryparse?view=netframework-4.7.1#System_Int32_TryParse_System_String_System_Int32__) instead, since you don't know if the value is really a number – Gabriel Luci Apr 13 '18 at 14:54
  • @LongLuong, the input is Ivan 24 1192 1500.353 – Yaroslav Dokuzov Apr 13 '18 at 14:56
  • 1
    You got an exception but you made no effort to find out what it means. You don't even know which line threw the exception. You pass strings to `int.Parse()` and `double.Parse()`, but you don't appear to have any idea what strings you're passing. You can't find a mistake because you didn't try to look for one. The exception message is not very informative, but Google will tell you exactly what it means. Didn't you bother? – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 14:57
  • @YaroslavDokuzov Did you actually type that all on one line, like you say? – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 14:58
  • @YaroslavDokuzov you have to type your inputs separately. like hiting _enter_ – Long Luong Apr 13 '18 at 15:00
  • @EdPlunkett no, i didn't – Yaroslav Dokuzov Apr 13 '18 at 15:01
  • @YaroslavDokuzov You didn't what? – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 15:02
  • I didn't type all the input on one line – Yaroslav Dokuzov Apr 13 '18 at 15:03
  • 1
    Please read [MCVE] guide on posting code - in this case you should provide exact input as constant string and not `ReadLine`. Also find more suitable search engine than http://www.disney.com. Google, Bing would instantly give you explanation of the exception, searching on MSDN or just clicking F1 in Visual Studio would have helped with explanation of `Console.WriteLine` or `Int32.Parse`. So far this post does not bring any new information for SO in addition to thousands existing variant of this excepotn. – Alexei Levenkov Apr 13 '18 at 15:04
  • @AlexeiLevenkov why do you think I use disney.com??? – Yaroslav Dokuzov Apr 13 '18 at 15:09
  • @YaroslavDokuzov Go back and think about my first comment, and Alexei's most recent comment. 90% of programming problems are just like this, and they're incredibly easy to solve: 1) Find out what the exception means (easy); 2) Find out what line it was thrown on (easy); 3) Find out **what the actual inputs are on that line** -- all of them, and I mean what they **really actually are**, not just what you want to assume they are, or what you wish they were. Use the debugger. Assign the return value from `Console.ReadLine()` to a temp variable before using it. so you can watch it. – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 15:12
  • :) Because it has search bar too... and it is more or less only place you could not find explanations of this message. Obviously it was not very serious statement... I'm quite sure you *did not even tried*, but saying that would be "not nice" (and that's what downvotes without comments are for), but since you asked... – Alexei Levenkov Apr 13 '18 at 15:16
  • @AlexeiLevenkov He thinks he tried, but he doesn't know *how* to try. If nobody tells him, how will he find out? Downvotes are very hard to interpret. – 15ee8f99-57ff-4f92-890c-b56153 Apr 13 '18 at 15:38

1 Answers1

0

There is something wrong with whatever your inputting. So you may be trying to input a double into an int which would throw this error. Try using int.TryParse().

forwardi
  • 26
  • 4