-4

I have a problem with converting string array (previously loaded from .txt file) to integer one. File has got 100 random numbers, they load without any problem, I just need to convert them to integers to make them sortable with 3 types of sorting. I've tried many thing that were said here, but none of them seem to work. All the time I'm getting an error saying that it can't be converted. Here is my loading from the file code:

string[] path = File.ReadLines("C:\\Users\\M\\numb.txt").ToArray();
int[] numb= new int[path.Length];

for (int i = 0; i < path.Length; i++)
{
    Console.WriteLine(path[i]);
}

And after some options to choose I'm using switch to pick one:

switch (a)
{
    case 1:
        Console.WriteLine("1. Bubble.");
        //int[] tab = numb; 
        babel(path);

        for (int z = 0; z < path.Length; z++)
        {
            Console.Write(path[z] + ", ");
        }
        break;

I've got bubble sorting method in my program too, don't think it is necessary to post it here.

If anyone can help me here, I'd be really grateful.

@Amy - I've tried this:

numb[i] = path[i].Convert.toInt32(); - it doesn't work.

What I want to achieve is to change every number in this array to int, I think it should be involved here:

{
    Console.WriteLine(path[i]);
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
pisul
  • 1
  • 1

3 Answers3

0

This conversion works.

#string[] path = File.ReadLines("C:\\Users\\M\\numb.txt").ToArray();    
String[] path = {"1","2","3"};
int[] numb = Array.ConvertAll(path,int.Parse);

for (int i = 0; i < path.Length; i++)
{
    Console.WriteLine(path[i]);
}

for (int i = 0; i < numb.Length; i++)
{
    Console.WriteLine(numb[i]);
}
webmite
  • 575
  • 3
  • 6
  • Actually there might be a problem if any of strings contains a letter or another sign. – suchoss May 25 '18 at 20:57
  • provider did not state that there was a need to qualify the input numbers. That may have occurred before the text file was created. I am only providing the solution with minimal steps. – webmite May 25 '18 at 21:14
0

It is better to use TryParse instead of parse. Also it is easier to work with List, than with array.

using System;
using System.Collections.Generic;

namespace StringToInt
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] path = { "1", "2", "3", "a", "b7" };
            List<int> numb = new List<int>();


            foreach (string p in path)
            {
                if (int.TryParse(p, out int result))
                {
                    numb.Add(result);
                }
            }

            for (int i = 0; i < path.Length; i++)
            {
                Console.WriteLine(path[i]);
            }

            for (int i = 0; i < numb.Count; i++)
            {
                Console.WriteLine(numb[i]);
            }
        }
    }
}
suchoss
  • 3,022
  • 1
  • 19
  • 21
-2

I can't imagine this wouldn't work:

string[] path = File.ReadAllLines("C:\\Users\\M\\numb.txt");
int[] numb = new int[path.Length];

for (int i = 0; i < path.Length; i++)
{
    numb[i] = int.Parse(path[i]);
}

I think your issue is that you are using File.ReadLines, which reads each line into a single string. Strings have no such ToArray function.

Nolan B.
  • 427
  • 1
  • 4
  • 9
  • This one works, it shows all the numbers on the screen as it should, I now need to sort them and that's where the problem starts since I can't sort them as strings/better to convert whole array to integer to do it. – pisul May 25 '18 at 17:54
  • Hence why I've converted to an `int[]`, which can be sorted. – Nolan B. May 25 '18 at 18:53