2013-03-19 56 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