简体   繁体   English

将字符串数组转换为int数组'System.FormatException'

[英]Convert string array to int array 'System.FormatException'

I try to convert the data1 string array to an int array end maybe there is some other solutions for this task but if its possible I want to make this working. 我尝试将data1字符串数组转换为int数组末尾,也许有其他解决方案可以完成此任务,但是如果可能的话,我想使它工作。

Problem is when I start the problem its got a stops and drops me the following problem: "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll" I ryed also with int.parse same problame. 问题是,当我开始该问题时,它停了下来,并向我抛出以下问题:“ mscorlib.dll中发生了'System.FormatException'类型的未处理异常”,我也用int.parse同样的道歉。

static int[] data()
            {
                StreamReader house = new StreamReader("text.txt");
                while (!house.EndOfStream)
                {
                    s = house.ReadLine();
                    Console.WriteLine(s);
                }
                string[] data1 = s.Split(' ');
                int[] database = new int[(data1.Length)];
                for (int j = 0; j < data1.Length; j++)
                {
                    database[j] = Convert.ToInt32(data1[j]);//Program stops here
                }
                return database;
            }

text.txt looks like this(digits separated with space " "): text.txt看起来像这样(数字之间用空格“”分隔):

6 1 1
10 5 10
20 10 20
35 5 15
45 5 5 
60 10 25 
75 5 10 

Thank you for your help! 谢谢您的帮助!

Probably an empty string get into your array of splitted strings. 可能是空字符串进入了拆分字符串数组。

Try defining StringSplitOptions when doing the split: 尝试在拆分时定义StringSplitOptions

 string[] data1 = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);

You can also check for empty string inside your loop: 您还可以在循环中检查空字符串:

for (int j = 0; j < data1.Length; j++)
{
     if (string.IsNullOrWhitespace(data1[j])
         continue;
     database[j] = Convert.ToInt32(data1[j]);//Program stops here
}

You can use Int32.TryParse. 您可以使用Int32.TryParse。 But if the conversion fails, you have an array item more than expected. 但是,如果转换失败,则您有比预期更多的数组项。 Therefore, it is better to use a List. 因此,最好使用列表。 And also, you are performing the conversion only for the last line of your file. 而且,您仅对文件的最后一行执行转换。 A '{' is located wrong. '{'位于错误的位置。 Last but not least, you should Disponse() the stream reader object. 最后但并非最不重要的一点是,您应该Disponse()流阅读器对象。

            static int[] data()
            {
                List<int> database = new List<int>();
                StreamReader house = new StreamReader("text.txt");
                while (!house.EndOfStream)
                {
                    s = house.ReadLine();
                    Console.WriteLine(s);

                    string[] data1 = s.Split(' ');
                    for (int j = 0; j < data1.Length; j++)
                    {    
                        int value;
                        if (Int32.TryParse(data1[j], out value))
                            database.Add(value));
                    }       
                }
                house.Dispose();
                return database.ToArray();
            }

Have you tried int Integer.parseInt(string) ? 您是否尝试过int Integer.parseInt(string)

database[j] = Integer.parseInt(data1[j]);//Program stops here

Also, I would check carefully what the contents of those chopped up strings are (eg, has a new-line character creeped in, is the last line blank...), so display them surrounded by another character, like " or [] ... 另外,我会仔细检查这些切碎的字符串的内容(例如,是否有换行符,最后一行是空白...),因此将其显示为另一个字符,例如“或[]” ...

暂无
暂无

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

相关问题 无法将字符串转换为int:&#39;System.FormatException&#39; - Can't convert string to int: 'System.FormatException' System.FormatException:“无法将参数值从 String 转换为 Int32。” - System.FormatException: 'Failed to convert parameter value from a String to a Int32.' 字符串上Int32上的System.FormatException - System.FormatException on Int32 from string 尝试将String转换为Double时发生System.FormatException - System.FormatException when trying to convert String to Double 尝试将字符串转换为整数会导致System.FormatException类型的错误 - Trying to convert a string to integer causes a error of type System.FormatException MVC将Base64字符串转换为Image,但是...... System.FormatException - MVC Convert Base64 String to Image, but … System.FormatException System.FormatException:“Base-64 字符数组或字符串的长度无效。” - System.FormatException: 'Invalid length for a Base-64 char array or string.' 获取未处理的异常:System.FormatException:当数组中不包含字符时,字符串的长度必须恰好是一个字符 - Getting Unhandled Exception: System.FormatException: String must be exactly one character long when a character is not contained in an array 附件解密 System.FormatException:Base-64 字符数组或字符串的长度无效 - Attachment Decryption System.FormatException: Invalid length for a Base-64 char array or string System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM