【问题标题】:How do I read and separate the first portion of the array from the rest?如何读取数组的第一部分并将其与其余部分分开?
【发布时间】:2014-12-14 07:47:36
【问题描述】:

所以我的代码有一个小问题,几乎可以肯定它应该很容易解决。所以我在一个文件中有一些数据:

25 150
60
63
61
70
72
68
66
68
70

这部分数据的问题是第一行:“25 150”,假设保存为整数,以便我可以在整个代码中使用它们。我已经解决了没有那行数字的问题,因为数组可以像往常一样将它们分开。如何编写代码以便将这两个数字分开并将它们保存为两个不同的整数?这是我到目前为止的代码:

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

namespace proj11LEA
{
    class Program
    {
        const int SIZE = 50;

        static void Main(string[] args)
        {
            int[] volts = new int[SIZE];

            string environment = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\";

            Console.WriteLine("\nResistor Batch Test Analysis Program\n");
            Console.WriteLine("Data file must be in your Documents folder.");
            Console.WriteLine("Please enter the file name: ");
            string input = Console.ReadLine();

            string path = environment + input;

            StreamReader data = new StreamReader(path);
            string fromFile;
            int count = 0;
            int start = 1;

            Console.WriteLine("\nRes#\tDissipation\tPassed");

            do
            {
                fromFile = data.ReadLine();
                if (fromFile != null)
                {
                    string[] dataArray = fromFile.Split();
                    volts[count] = int.Parse(dataArray[0]);
                    count++;
                }
            } while (fromFile != null);

            for (int i = 0; i < count; i++ )
            {
                int diss = (volts[i] * volts[i])/25;
                if (diss >= 150)
                {
                    string answer = ("yes");
                    Console.WriteLine("{0}\t{1:d2}\t\t{2}", start++, diss , answer);
                }
                else
                {
                    string answer = ("no");
                    Console.WriteLine("{0}\t{1:d2}\t\t{2}", start++, diss, answer);
                }
            }
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 要清楚 - 你在一个文件中拥有所有这些数字,并且你想取第一行并创建它们的两个整数?你当前的代码是做什么的?

标签: c# arrays file split


【解决方案1】:

这将根据你所拥有的数字文本文件创建一个 int 列表,然后你可以根据需要对其进行操作!

   List<int> data = new List<int>();

        using (StreamReader xReader = new StreamReader("TextFile1.txt"))
        {
            string line;

            while (!xReader.EndOfStream)
            {
                line = xReader.ReadLine();
                string[] input = line.Split(' ', '\r');// this splits the line on the space and newline
                foreach (string item in input)
                {
                    data.Add(Convert.ToInt32(item));
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2010-09-08
    • 2019-09-09
    • 2021-09-23
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多