2017-07-11 65 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()回報單個字符,這就是爲什麼

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

看起來很suspecious;另一個問題是,並非每個字符串是一個正確的整數值("bla-bla-bla"就是一個例子):

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

讓我們重新寫的片段:

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

string namet = Console.Read().ToString();同樣的修改也應該是

// 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

可能是您在輸入後插入的額外空格或字符。所以你應該刪除該空間或額外的字符,如

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

試試看,它應該工作。

感謝

0

你必須使用Console.ReadLine()

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

,或者您可以使用int.TryParse象下面這樣:

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

它會在內部處理異常,將默認值0返回如果發現任何異常轉換。

你可以閱讀更多有關int.TryParsehere

希望它可以幫助你。

+0

獲取異常比讓框架吞下它更好。我會用'Convert.ToInt32'而不是'int.TryParse'。 – niksofteng

相關問題