2017-07-11 9 views
0

1.iを入力して辞書に保存し、再度入力を取得して、辞書に含まれているかどうかを確認する必要があります。
2.これを試しましたが、エラーが発生しました(入力文字列が正しい形式ではありません)。
3.コンパイル時にエラーを出すことはありませんが、実行時にエラーが発生しています。これに関する問題があります。
4.私の入力は以下のとおりです。 - SAM 99912222
トム11122222
ハリー12299933
SAM
エドワード
ハリー・リードの代わりに
5.()。 ReadLine()も試みましたが、問題は同じです。C#コードでランタイムエラーが発生する

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"); 
      } 
     } 

    } 

} 

完全なエラーが

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 
+0

あなたの入力は何ですか? –

+1

'Console.ReadLine()によって入力されたものがintに変換できない可能性があります –

+0

@HiranPerera入力で質問を再編集しました。 – shindvii

答えて

1

まあ、Console.Read()戻り単一のcharある

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

は非常にsuspeciousに見える理由、それはです。それは

あるべき string namet = Console.Read().ToString();と同じ改正

// 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."); 
} 

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

レッツ・再書き込みフラグメント:別の問題がなく、すべての文字列が正しい整数値("bla-bla-bla"は一例である)であるということです

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

編集:

になるだろう

あなたはsam 99912222を入れて、

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

Console.Read()はちょうど最初の文字's'(ない"sam")と他の部分"am"Convert.ToInt32(Console.ReadLine());によって読み取られる読み込み実行します。確かに"am"有効な整数ではありませんと例外があります。

+0

@Dimitry Bychenko質問を再編集してください。 – shindvii

0

Enterキーを押した後に余分なスペースや文字が挿入されることがあります。そのようなスペースや余分な文字を削除する必要があります

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

試してみるとうまくいくはずです。あなたはConsole.ReadLine()

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

を使用する必要がありますかあなたは以下のようなint.TryParseを使用することができます

おかげ

0

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

それは内部例外を処理するには、それはデフォルト値0で返します。変換に例外が見つかった場合。

あなたはint.TryParsehere詳細を読むことができます:

は、それはあなたのお役に立てば幸いです。

+0

フレームワークがそれを飲み込ませるよりも例外を取得する方が良いです。 'int.TryParse'の代わりに' Convert.ToInt32'を使ってみましょう。 – niksofteng

関連する問題