【问题标题】:Parsing a file with strings and integers c#用字符串和整数解析文件c#
【发布时间】:2016-11-17 10:45:36
【问题描述】:

我接到了一个简单的任务,但我似乎不知道如何完成它。

我收到了一个文本文件,其中包含员工的姓名和工资率/小时数。格式如下:

Mary Jones
12.50 30
Bill Smith
10.00 40
Sam Brown
9.50 40

我的任务是编写一个程序,使用 StreamReader 从文本文件中提取数据,然后打印员工姓名,并通过乘以费率和小时数来计算总工资。

我知道如何使用 .Split 方法拆分行,但我似乎无法弄清楚如何将名称与双精度/整数分开。我的解析方法总是返回格式错误,因为它首先读取字符串。我完全被卡住了。

到目前为止,这是我的代码,任何帮助或指导将不胜感激。

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

namespace lab21
{
    class Program
    {
        static void Main(string[] args)
        {

            StreamReader myfile = new StreamReader("data.txt");
            string fromFile;

            do
            {
                fromFile = myfile.ReadLine();
                if (fromFile != null)
                {
                    string[] payInfo = fromFile.Split( );
                    double wage = double.Parse(payInfo[0]);
                    int hours = int.Parse(payInfo[1]);
                    Console.WriteLine(fromFile);
                    Console.WriteLine(wage * hours);
                }
            } while (fromFile != null);
        }
    }
}

【问题讨论】:

    标签: c# streamreader


    【解决方案1】:

    您只在循环中读取一行。员工记录似乎由 两行 行组成 - 因此您需要在每次迭代时阅读这两行。 (或者,您可以跟踪您正在执行哪一行,但这会很痛苦。)我会将循环重写为:

    string name;
    while ((name = reader.ReadLine()) != null)
    {
        string payText = reader.ReadLine();
        if (payText == null)
        {
            // Or whatever exception you want to throw...
            throw new InvalidDataException("Odd number of lines in file");
        }
        Employee employee = ParseTextValues(name, payText);
        Console.WriteLine("{0}: {1}", employee.Name, employee.Hours * employee.Wage);
    }
    

    然后有一个单独的方法来解析这两个值,这样会更容易测试。

    在解析时,请注意您应该使用decimal 而不是double 来表示货币值。

    【讨论】:

    • 和我发生的一模一样。问题中的代码一次只能读取一行。这意味着当他读取员工的姓名时,它最终也会被解析为整数。但是,如果 OP 试图在 VS 中使用调试器,这种事情就会被捕获。我想知道他是否知道如何使用它?
    【解决方案2】:

    使用Decimal.Parseread two line

    do
    {
        name = myfile.ReadLine();
        if (name != null)
        {
            // read second line
            var nums = myfile.ReadLine();
            if (nums != null)
            {
                string[] payNums = nums.Split(new[] {' '});
                Console.WriteLine("{0}: {1}", 
                                  name,
                                  Decimal.Parse(payNums[0])
                                  * Decimal.Parse(payNums[1]));
            }
        }
    } while (name != null);
    

    【讨论】:

    • 你为什么要用名字分割一行,然后叫它payInfo?这是误导。我还建议将“阅读”与“解析”分开,使代码更灵活,更易于测试。
    【解决方案3】:

    您应该使用 int.TryParse(string, out int)(int 和 double)。如果失败,你可能有一个字符串,否则,你很幸运。

    有了这些数据,你知道每一行都是一个字符串,你应该把它也放在代码中,你可以有一个索引/计数,当它不均匀时,你应该期待一个字符串。

    【讨论】:

      【解决方案4】:

      你也可以试试这个。非常简单的修改。

      do
      {
          fromFile = myfile.ReadLine();
          fromFile += @" " + myfile.ReadLine();
          if (fromFile != null)
          {
               string[] payInfo = fromFile.Split( );
               double wage = double.Parse(payInfo[2]);
               int hours = int.Parse(payInfo[3]);
               Console.WriteLine(fromFile);
               Console.WriteLine(wage * hours);
          }
      } while (fromFile != null);
      

      【讨论】:

        猜你喜欢
        • 2010-10-03
        • 1970-01-01
        • 2011-04-03
        • 2010-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        相关资源
        最近更新 更多