从csv文件中读取数据以存储在int列表c#windows窗体中

本文关键字:int 列表 存储 c#windows 窗体 csv 文件 读取 数据 | 更新日期: 2023-09-27 18:28:59

我目前正试图为大学创建一个windows表单应用程序,以计算和显示从csv文件中读取的邮费。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace ParcelEXDelivery
{
    public partial class frmParcelCalculator : Form
    {
        int price, insurance;
        string file;
        List<int> UK = new List<int>();
        List<int> RestOfEurope = new List<int>();
        List<int> Worldwide = new List<int>();

        public frmParcelCalculator()
        {
            InitializeComponent();

        }
        private void destination()
        {
            if (rbUK.Checked)
            {
                file = "..''Debug''PostageCosts.csv";
                var reader = new StreamReader(file);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(',');
                    UK.Add(int.Parse(values[0]));
                }
                reader.Close();
                display();
            }
        }
        public void display()
        {
            label2.Text = UK[price].ToString();
        }

我知道这虽然很简单,但我不擅长编码。程序正在为带来一个错误

UK.Add(int.Parse(values[0]));

我能做些什么来解决这个问题?还是我把这个放错了?我试图在标签中显示,只是想看看是否正确的邮费是输出

16.5    27.3    41.6    52.4    66.2    0.6 per kg  UK
21.5    31.3    45.1    58.5    71.5    0.7 per kg  Rest of Europe
27.4    37.2    50.3    62.4    76.3    0.75 per kg Worldwide

这是csv文件

private void destination()
        {
            if (rbUK.Checked)
            {
                var file = "..''Debug''Questions.csv";
                if (File.Exists(file))
                {
                    var f = File.OpenRead(file);
                    var reader = new StreamReader(f);
                    while (!reader.EndOfStream)
                    {
                        var row = reader.ReadLine();
                        if (row != null)
                        {
                            var values = row.Split(',');
                            foreach (var item in values)
                            {
                                if (decimal.TryParse(values[0], out Value))
                                {
                                    UK.Add(Value);
                                }
                                else
                                {
                                }
                            }
                        }
                    }
                }
            }
        }

上面和下面的编辑方法是当我试图将值输出到标签时的代码,它说你不能从十进制转换为int

        public void display()
        {
            label2.Text = UK[Value].ToString();
        }

从csv文件中读取数据以存储在int列表c#windows窗体中

是的,您设置得非常错误。主要问题在于你的价值观。您要解析的是十进制数字,而不是整数。所以你的清单应该反映出这个基本条件。创建List<decimal>而不是List<int>

List<decimal> UK = new List<decimal>();
List<decimal> RestOfEurope = new List<decimal>();
List<decimal> Worldwide = new List<decimal>();

那么,解析部分并没有太大的错误。如果您的文件只包含根据您的区域性格式化的十进制数字(小数点是点还是逗号?),您可以使用decimal.Parse轻松地将其读回,但正如其他答案中所解释的那样,最好使用防御方法并调用decimal。TryParse没有意外(而且很容易避免)的异常。

  decimal curValue;
  if(decimal.TryParse(values[0], out curValue))
     UK.Add(curValue);
  //else
  //   ... if you want some kind of error reporting here ...

编辑

在你阅读文件的方式上也有一些问题需要解决
看起来你有三行,第一行的值应该在第一个列表(英国)中,第二行的值应在第二个列表(RestOfEurope)中,以及全球列表中的第三行。我建议一起阅读所有行,然后在适当的列表中分配值

private void destination()
{
    if (rbUK.Checked)
    {
        // Read all the lines in a single call, 
        string[] lines = File.ReadAllLines(@".'Debug'PostageCosts.csv");
        // I expect to have three lines but better to check before acting on the array
        if(lines.Length > 0)
        {
            // Split the line for UK and use only the first 6 values
            // for the list (the last strings 'per kg  UK' should not be added)
            // Again, at least 6 decimal values are expected not less...
            var values = lines[0].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    UK.Add(decimal.Parse(values[x]));
        }
        if(lines.Length > 1)
        {
            var values = lines[1].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    RestOfEurop.Add(decimal.Parse(values[x]));
        }
        if(lines.Length > 2)
        {
            var values = lines[2].Split(',');
            if(values.Length >= 6)
                for(int x = 0; x < 6; x++)
                    WorlWide.Add(decimal.Parse(values[x]));
        }
        // Test what list to display passing the index 0,1,2
        int listToShow = 0;
        display(listToShow);
    }
}
public void display(int listToShow)
{
    List<decimal> showList = (listToShow == 0 ? UK : 
                              listToShow == 1 ? RestOfEurope : 
                                                WorlWide);
    label2.Text = string.Join("-",  showList.ToArray());
}