2013-03-19 44 views
-1

我正在嘗試使用Stream Writer和Stream Reader將數據輸入到文本文件以輸出信息,在這種情況下 - 名字,姓氏&年齡。然後,我希望將這些信息輸出到如下表格中:文件處理中的錯誤

First Name   Surname    Age 
Etc     Etc     Etc 

但是,我遇到了一些問題!

請參閱下面的代碼,任何人都可以解釋爲什麼我得到以下錯誤消息?

錯誤1只使用未分配的局部變量 '姓名'

錯誤2使用未分配的局部變量的 '名字'

錯誤3使用未分配的局部變量 '年齡'

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{// Start namespace 
    class Program 
    {// Start Class 
     static void Main(string[] args) 
     {// Start Main 
      // Declaration Strings 

      String firstname; // Declares Name as a String 
      String lastname; // Declares Surname as a String 
      String age; // Declares Age as a String 
      int Counter; // Declares Counter as an Integer 

      FileInfo fleNames = new FileInfo("Names.txt"); // Creates fleNames as an Object, using Names.txt as the file name 
      StreamWriter swrNames = fleNames.CreateText(); // Informs fleName to Create Text 

      for (Counter = 0; Counter < 1; Counter++) 
      { 
       Console.Write("Enter Your First Name:"); // Writes users first name using Stream Writer Command 
       firstname = Console.ReadLine(); 
       swrNames.WriteLine(firstname); // This dictates where the first name will be stored in the text file 
       swrNames.Flush(); 

       Console.Write("Enter Your Surname"); // Writes users first name using Stream Writer Command 
       lastname = Console.ReadLine(); 
       swrNames.WriteLine(lastname); // This dictates where the last name will be stored in the text file 
       swrNames.Flush(); 


       Console.WriteLine("Enter Your Age"); // Writes users first name using Stream Writer Command 
       age = Console.ReadLine(); 
       swrNames.WriteLine(age); // This dictates where the age will be stored in the text file 
       swrNames.Flush(); 
       Console.Clear(); 


      } 


      swrNames.Close(); 

      String NamesTable; 

      StreamReader swreNames = File.OpenText("Names.txt"); 

      while ((NamesTable = swreNames.ReadLine()) != null) 
      { 
       Console.WriteLine(NamesTable); 

      } 

      swreNames.Close(); 

      Console.ReadLine(); 

      FileInfo fleNamesTwo = new FileInfo("NamesTwo.txt"); 

      StreamWriter swrNamesTwo = null; 


      // ------------ CHECK APPEND TO A FILE -------------- 

      if (fleNames.Exists == true) 
      { 
       swrNames = fleNames.AppendText(); 
      } 
      else // -- Create a new text file 
      { // Declare Strings 
       String Name; // Name 
       String Surnames; // Surname 
       String Ages; // Age 
       swrNamesTwo = fleNamesTwo.CreateText(); 

       Console.Write("Enter Your First Name"); 
       Name = Console.ReadLine(); 
       swrNamesTwo.WriteLine(Name); 
       swrNamesTwo.Flush(); 

       Console.Write("Enter Your Surname"); 
       Surnames = Console.ReadLine(); 
       swrNamesTwo.WriteLine(Surnames); 
       swrNamesTwo.Flush(); 


       Console.WriteLine("Enter Your Age"); 
       Ages = Console.ReadLine(); 
       swrNamesTwo.WriteLine(Ages); 
       swrNamesTwo.Flush(); 
       Console.Clear(); 


      } 

      Console.Clear(); 


      Console.SetCursorPosition(15, 2); 

      Console.Write("--- Names Table ---"); 

      Console.SetCursorPosition(10, 4); 

      Console.Write("First Name"); 

      Console.SetCursorPosition(28, 4); 

      Console.Write("Surname"); 

      Console.SetCursorPosition(48, 4); 

      Console.Write("Age"); 

      Console.ReadLine(); 

      do 
      { 
       //Read a File a Streamreader Command 

       swrNames.WriteLine(firstname); // Reads from first name from file Names.txt 
       Console.SetCursorPosition(10, Counter + 6); // Aligns first name within table settings 

       swrNames.WriteLine(lastname); // Reads from last name from file Names.txt 
       Console.SetCursorPosition(28, Counter + 6); 

       swrNames.WriteLine(age); // Reads from age from file Names.txt 
       Console.SetCursorPosition(48, Counter + 6); 
       Console.WriteLine("{0} {1} is {2} years old", firstname, lastname, age); 


       Console.Clear(); //Clears Screen 
       Console.ReadLine(); 



      } while ((firstname = swreNames.ReadLine()) != null); //Writes out the input from the text file 
     } 
    } 
} 

好吧,讓你有幫助的人讓我過去的錯誤!謝謝:)

但是,(總是有一個!!!) 我仍然無法讓流編寫器/閱讀器在表中顯示結果。我得到的結果顯示在表格後面,但表格是空白的,除了表格標題全部在整齊的佔位符中! ANYONE ..... :) :) :)

+0

初始化變量,爲空或的String.Empty – NoviceProgrammer 2013-03-19 14:39:58

+0

你有沒有想過'Initializing'字符串變量='string.Empty' '字符串名字=的String.Empty; //將名稱聲明爲字符串' 'string lastname = string.empty; //聲明姓氏爲字符串' 'string age = string.empty; // De' – MethodMan 2013-03-19 14:41:29

+2

試圖理解這是什麼...它需要多個修復。爲什麼要有循環:'for(Counter = 0; Counter <1; Counter ++)'?它只會執行一次。 – 2013-03-19 14:41:29

回答

1

在C#中,錯誤Use of unassigned local variable意味着您正在嘗試使用您尚未給出值的變量。與其他一些語言不同,C#要求您保證在使用變量之前已經爲變量指定了一些內容(即使爲null) - 它不會將變量初始化爲默認值(如VB.NET),也可能會使用垃圾值(如C++)。

在你的情況下,firstname填充在for循環中,這意味着如果for循環的主體從不執行,它可能永遠不會獲得值。當您嘗試在do內使用它時,可能它從未被分配過。

爲了解決這個問題,最常見的方法是在聲明變量時將其設置爲null,這意味着您已經保證在您使用它時會有一些東西。當然,確保你正確地處理了如果變量一直保持爲空而發生的情況。

2

你會通過你的字符串變量設置爲默認值開始:

 String firstname = string.empty; 
     String lastname = string.empty; 
     String age = string.empty; 

然後,如果你的循環將不會執行,您的變量將有一個初始值。當在後面的代碼訪問的,不會發生錯誤

ADDING編輯根據註釋,初始化計數器:

int Counter=0; 
+0

也可能初始化'int'變量以及 – MethodMan 2013-03-19 14:45:22

+0

@DJ ...好點。 – MikeTWebb 2013-03-19 14:47:20

+0

+1只是試圖確保OP保持一致lol – MethodMan 2013-03-19 14:48:51

0

編譯器是不是足夠聰明,知道這個循環:

for (Counter = 0; Counter < 1; Counter++) 

將始終只執行一次。所以它必須假定有可能代碼根本不會執行,在這種情況下,這些變量永遠不會被初始化。

如果您的for循環實際上只打算執行一次,那麼只需刪除for循環,變量將在使用前被賦值。

如果計數器只是用於測試,你打算執行for循環多次,然後初始化varialbes到一些虛擬起始值(null""等)

0

嘗試清理你的代碼(你在用戶輸入之前不需要for循環,否則它將只執行一次)。 稍微更改您的代碼,以便您可以檢查Console.ReadLine()返回的內容。 此外,錯誤看起來像它正在發生,因爲變量在某處使用,但他們可能是未分配(如果不滿足條件)。 聲明它們時給它們一個值,錯誤應該消失。順便說一句,考慮重構你的代碼,並在一個或多個方法中分割主。

string firstname = string.empty; // And so on.. 
+0

感謝您的幫助..我到了那裏..仍然不能讓任何東西出現在我的表格中! :( – 2013-03-19 14:59:47