简体   繁体   中英

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.

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.

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 " "):

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:

 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. 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.

            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) ?

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 [] ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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