2017-06-07 48 views
0

我有两个文本文件匹配字符串[]:有C#控制台应用程序,搜索来自两个文本文件

number,letter,color,animal 
1,a,green,alligator 
2,b,brown,bear 
3,c,black,cat 
4,d,white,dog 
5,e,pink,elephant 

colour,animal,found,diet 
green,alligator,swamp,fish 
green,alligator,swamp,bird 
brown,bear,forest,fruit 
black,cat,home,catfood 
white,dog,home,dogfood 
pink,elephant,space,spacefruit 

我的代码animal.txt和habit.txt到目前为止要求一个号码和一封信。并使用字符串[]和分割搜索文本文件。

class Program 
{ 
    static void Main(string[] args) 
    { 
     int counter = 0; 
     string line; 
     string number; 
     string letter; 
     bool lineFound = false; 

     do 
     { 
      Console.WriteLine("Enter number"); 
      number = Console.ReadLine(); 

      Console.WriteLine("\nEnter letter"); 
      letter = Console.ReadLine(); 

      System.IO.StreamReader file = new System.IO.StreamReader("animal.txt"); 
      while ((line = file.ReadLine()) != null) 
      { 
       string[] words = line.Split(','); 
       if ((number == words[1]) && (letter == words[0])) 
       { 
        Console.WriteLine(line); 
        lineFound = true; 
       } 

       counter++; 
      } 

      if (!lineFound) 
      { 
       Console.WriteLine("Invalid number and/or letter"); 
      } 

      file.Close(); 

     } 
      while (!lineFound); 

取决于输入它会显示颜色和动物的行。 如何制作它,以便它将搜索另一个文件,habit.txt以查找animal.txt中的匹配行。例如输入可以是“1”和“a”,控制台将显示

green,alligator,swamp,fish 
green,alligator,swamp,bird 

回答

0
String.Contains("some text") 

我建议使用一个数据库,为您的脚本所需的数据,而不是像长站起来.text的文件字符串。

0
class Program 
{ 
    static void Main(string[] args) 
    { 
     int counter = 0; 
     string line; 
     string number; 
     string letter; 
     // this assumes that there will always be only one record?? 
     bool lineFound; 

     string animalType = null; 
     string animalColor = null; 

     do 
     { 
      Console.WriteLine("Enter number"); 
      number = Console.ReadLine(); 

      Console.WriteLine("\nEnter letter"); 
      letter = Console.ReadLine(); 

      System.IO.StreamReader file = new System.IO.StreamReader("animal.txt"); 
      while ((line = file.ReadLine()) != null) 
      { 
       string[] words = line.Split(','); 
       // your example data shows the number in position 0 and the letter in position 1 
       if ((number == words[0]) && (letter == words[1])) 
       { 
        Console.WriteLine(line); 

        // assign the found animals type and color to use later 

        animalType = words[3]; 
        animalColor = words[2]; 

        lineFound = true; 
       } 

       counter++; 
      } 

      if (!lineFound) 
      { 
       Console.WriteLine("Invalid number and/or letter"); 
      } 

      file.Close(); 

      System.IO.StreamReader habitatFile = new System.IO.StreamReader("habitat.txt"); 
      // line is being used by two different streams and can create unexpected behavior so instead create a seperate variable 
      while ((line = habitatFile.ReadLine()) != null) 
      { 
       string[] words = line.Split(','); 
       // your example data shows the number in position 0 and the letter in position 1 
       if ((animalColor == words[0]) && (animalType == words[1])) 
       { 
        Console.WriteLine(line); 
       } 
      } 

      habitatFile.Close(); 
     } 
     while (!lineFound); 
    } 
} 

不过,我会考虑在寻找使这些行文本类的,看你能做出什么事情更容易(例如,如果你能代替按颜色搜索什么动物?)

public class Animal 
{ 
    public int Number { get; set; } 

    public string Letter { get; set; } 

    public string Color { get; set; } 

    // CSV file has listed as 'animal' 
    public string Name { get; set; } 
} 

public class Habitat 
{ 
    public string Color { get; set; } 

    // CSV file has listed as 'animal' 
    public string Name { get; set; } 

    public string Found { get; set; } 

    public string Diet { get; set; } 

} 
相关问题