//assign variables
string measurementIn;
//read in file in array
string[] lines = File.ReadAllLines("../../convert.txt");
//ask user for date
Console.WriteLine("Enter the conversion in the form (amount,from,to)");
measurementIn = Console.ReadLine();
//array for splitting input
string[] conversionArray = measurementIn.Split(',');
Console.WriteLine("{0} , {1} , {2}", conversionArray[0], conversionArray[1], conversionArray[2]);
Console.ReadLine();
//check lines for conversion
foreach (string line in lines)
{
//array for the measurement
string[] measurementArray = line.Split(',');
Console.WriteLine("{0}", measurementArray[0]);
问题出在最后,foreach里面的东西
答案 0 :(得分:3)
也许你会搜索这样的东西?
in Txt:
Dog,Cat,Mouse,Fish,Cow,Horse,Hyena
Programmer,Wizard,CEO,Rancher,Clerk,Farmer
代码:
using System;
using System.IO;
class Program
{
static void Main()
{
int i = 0;
foreach (string line in File.ReadAllLines("TextFile1.txt"))
{
string[] parts = line.Split(',');
foreach (string part in parts)
{
Console.WriteLine("{0}:{1}",
i,
part);
}
i++;
}
}
}
输出:
0:Dog
0:Cat
0:...
1:Programmer
1:...
我得到了它: http://dotnetperls.com/string-split
刚试了一下:
StreamReader convert = new StreamReader("../file.txt");
string line = convert.ReadLine();
String inputMeasurement = Console.ReadLine();
string[] inputMeasurementArray = inputMeasurement.Split(',');
while (line != null)
{
string[] fileMeasurementArray = line.Split(',');
if (fileMeasurementArray[0] == inputMeasurementArray[1])
{
if (fileMeasurementArray[1] == inputMeasurementArray[2])
{
Console.WriteLine("{0}", fileMeasurementArray[2]);
}
else
{
Console.WriteLine("False");
}
}
else
{
Console.WriteLine("False");
}
line = convert.ReadLine();
}
Console.ReadKey();
答案 1 :(得分:1)
尝试使用StreamReader从文件中读取行,而不是使用File.ReadAllLine(),然后循环遍历每一行。像这样:
StreamReader convert = new StreamReader("filename.txt");
string line = convert.ReadLine();
while (line != null)
{
string[] measurementArray = line.Split(',');
Console.WriteLine("{0}", measurementArray[0]);
Console.WriteLine("{0}", measurementArray[1]);
line = convert.ReadLine();
}
您需要添加“using System.IO;”。