繁体   English   中英

输入的字符串格式不正确将字符串加倍[] []数组

[英]Input string was not in a correct format String to double [] [] array

我知道这里有更多关于此主题的主题,但实际上没有一个对我有帮助。

我将提供完整的代码:

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[][]输入的内容:

166163180228

165162160226

166163180228

166164180228

171162111225

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

1 0 0

1 0 0

0 1 0

0 1 0

1 0 0

问题:文本文件中的每一行之后都有多余的空行。 当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());  
              }                
            }

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

它会有所帮助,因为您将获得空行,因此RemoveEmptyEntries将从Split方法的结果中删除这些行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM