0

1.i have to get input and store it to dictionary, and again get some input and check whether it is in dictionary or not.
2. I have tried this but giving the error (input string was not in correct format).
3. its not giving any error at compile time,but it is giving error at run time.whats the problem with this.
4. my inputs are: - 3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
5. In place of Read(). I have also tried ReadLine(), but the problem is same.

   using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
    static void Main()
    {

        int n = Convert.ToInt32(Console.Read());
        Dictionary<string, int> phbook = new Dictionary<string, int>();
        for (int i = 0; i < n; i++)
        {
            string name = Console.Read().ToString();
            int phonno = Convert.ToInt32(Console.ReadLine());
            phbook.Add(name, phonno);
        }

        foreach (var keypairs in phbook)
        {
            string namet = Console.Read().ToString();
            if (phbook.ContainsKey(namet))
            {
                Console.Write("{0}={1}", namet, phbook[namet]);
            }
            else
            {
                Console.Write("Not found");
            }
        }

    }

}

The complete error is

Unhandled Exception:
System.FormatException: Input string was not in a correct format.
  at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00015] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00008] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.Convert.ToInt32 (System.String value) [0x0000b] in <a07d6bf484a54da2861691df910339b1>:0 
  at Solution.Main () [0x00034] in solution.cs:15 
[ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: Input string was not in a correct format.
  at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00015] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00008] in <a07d6bf484a54da2861691df910339b1>:0 
  at System.Convert.ToInt32 (System.String value) [0x0000b] in <a07d6bf484a54da2861691df910339b1>:0 
  at Solution.Main () [0x00034] in solution.cs:15 
5
  • What was your input? Commented Jul 11, 2017 at 6:30
  • 1
    something u input by Console.ReadLine() might not able to convert to int Commented Jul 11, 2017 at 6:30
  • @HiranPerera i have re edited the question with inputs. Commented Jul 11, 2017 at 6:46
  • Get the whole line by ReadLine() and substring it from the last index of spaces. Could you please try that out and let me know Commented Jul 11, 2017 at 7:09
  • It is better practice to put edits under Edit heading so that already active users of the thread don't have to scroll though whole question content again. Commented Jul 11, 2017 at 7:36

3 Answers 3

1

Well, Console.Read() returns single char, that's why

 string name = Console.Read().ToString();

looks very suspecious; another issue is that not every string is a correct integer value ("bla-bla-bla" being an example):

 int phonno = Convert.ToInt32(Console.ReadLine());

Let's re-write the fragment:

 // We want name (string), say "Test" not just character 'T'
 string name = Console.ReadLine();

 int phonno = 0;

 // Ask for a number until a correct one is provided
 while (!int.TryParse(Console.ReadLine(), out phonno)) {
   Console.WriteLine("Incorrect number, please put the number again."); 
 } 

The same amendment with string namet = Console.Read().ToString(); it should be

 // We want name (string), say "Test" not just character 'T'
 string namet = Console.ReadLine();

Edit: What's going on

You put sam 99912222 and execute

string name = Console.Read().ToString();
int phonno = Convert.ToInt32(Console.ReadLine());

Console.Read() reads just the first character 's' (not "sam") and the other part "am" is read by Convert.ToInt32(Console.ReadLine());. Sure "am" is not a valid integer and you have the exception.

Sign up to request clarification or add additional context in comments.

1 Comment

@Dimitry Bychenko please review the question I have re edited it.
0

May be an extra space or character being inserted after you hit enter. So you should remove that space or extra character like

int phonno = Convert.ToInt32(Console.ReadLine().Trim());

Give it a try and it should work.

Thanks

Comments

0

You have to use Console.ReadLine()

int n = Convert.ToInt32(Console.ReadLine());

or you can use int.TryParse like below:

Int32 n=0;
int.TryParse(Console.ReadLine(), out n); 

it will handle the exception internally, it will return by default value 0 if any exception found into conversion.

you can read more about int.TryParse here :

Hope it helps you.

1 Comment

It is better to get an exception than to let framework swallow it. I would go with Convert.ToInt32 instead of int.TryParse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.