【问题标题】:Input string was not in a correct format String to double [] [] array输入字符串的格式不正确 String to double [] [] array
【发布时间】:2014-02-17 08:57:49
【问题描述】:

我知道这里有更多关于这个主题的主题,但它们都没有真正帮助我。

我会提供完整的代码:

namespace ConsoleApplication1
{
    public static class Load
    {
        public static double[][] FromFile(string path)
        {
            var rows = new List<double[]>();
            foreach (var line in File.ReadAllLines(path))
            {
                // HERE IS THE ERROR
                rows.Add(line.Split(new[] { ' ' }).Select(double.Parse).ToArray());  
            }
            return rows.ToArray();
        }
    }

    public class Program
    {

        static void Main( string [ ] args )
        {
            string cestain = @"E:\vstup.txt";
            double[][] innput = Load.FromFile(cestain);

            string cestaout = @"E:\vystup.txt";
            double[][] ooutput = Load.FromFile(cestaout);


            // c r e a t e a neural network
            var network = new BasicNetwork ( ) ;
            network . AddLayer (new BasicLayer ( null , true , 2) ) ;
            network . AddLayer (new BasicLayer (new ActivationSigmoid ( ) , true , 3) ) ;
            network . AddLayer (new BasicLayer (new ActivationSigmoid ( ) , false , 1) ) ;
            network . Structure . FinalizeStructure ( ) ;
            network . Reset( );

            // c r e a t e t r a i n i n g data
            IMLDataSet trainingSet = new BasicMLDataSet (innput , ooutput ) ;
            // t r a i n the neural network
            IMLTrain train = new ResilientPropagation (network , trainingSet) ;
            int epoch = 1 ;
            do
            {
                train.Iteration( ) ;
                Console.WriteLine(@"Epoch #" + epoch + @" Error : " + train.Error );
                epoch++;
            } while ( train.Error> 0.01 ) ;

            Console.ReadLine();
        }
    }
}

这是我要加载到double[][] 输入的内容:

166 163 180 228

165 162 160 226

166 163 180 228

166 164 180 228

171 162 111 225

这是我要加载到double[][] 输出的内容:

1 0 0

1 0 0

0 1 0

0 1 0

1 0 0

【问题讨论】:

  • 试试rows.Add(line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());
  • 学习使用调试器。

标签: c# multidimensional-array double file.readalllines


【解决方案1】:

问题:在文本文件中的每一行之后都有额外的 EmptyLines。 当 Split() 函数遇到新 Line 时,它​​会返回,因为没有可拆分的内容,并且 Add() 函数会抛出异常,因为空行不是有效的 Double

解决方案1:您可以使用StringSplitOptions.RemoveEmptyEntries 作为Split() 函数的第二个参数来忽略 EmptyLines。

        foreach (var line in File.ReadAllLines(path))
            {
                // HERE IS THE ERROR
                rows.Add(line.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());  
            }

解决方案2:您可以使用String.IsNullOrWhiteSpace()检查行是否为空

          foreach (var line in File.ReadAllLines(path))
            {
              if(!String.IsNullOrWhiteSpace(line))
              {
                // HERE IS THE ERROR
                rows.Add(line.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());  
              }                
            }

【讨论】:

    【解决方案2】:

    不久前我遇到了类似的问题。使用StringSplitOptions.RemoveEmptyEntries 为我解决了这个问题。所以它变成了,lines.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)

    这很有帮助,因为您得到的是空行,因此 RemoveEmptyEntries 会从 Split 方法的结果中删除这些行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2013-08-22
      相关资源
      最近更新 更多