2

I want to read the integers after the /. thats why i tried to use this code:

while ((line = file.ReadLine()) != null)
{
    string[] ip = line.Split('/');
    Console.WriteLine(ip[1]);                    
}

this code returns me this integers as string. I want to some math calculation with this numbers ,for example, multiplication. I tried this code but it doesnt work

int[] intArray = Array.ConvertAll(ip, int.Parse);
6
  • 4
    How doesn't it work? Does it explode? Commented Feb 9, 2014 at 14:00
  • I think one of items in ip array cannot be parsed as integer Commented Feb 9, 2014 at 14:02
  • If you try to convert ip-adresses, this wont work that simple. Commented Feb 9, 2014 at 14:02
  • output of the ip[1]=24,25,26,27 Commented Feb 9, 2014 at 14:03
  • You need to split up the ip itself since it is made up from 4 Int values, seperated by a dot Commented Feb 9, 2014 at 14:04

3 Answers 3

2

You are trying to convert each item of ip array to integer. At lease second item of it - "24 25 26 27" cannot be converted to single integer value. You should take this item of ip array, split it by white space and then parse each part to integer:

int[] intArray = ip[1].Split().Select(Int32.Parse).ToArray();

Or

int[] intArray = Array.ConvertAll(ip[1].Split(), Int32.Parse);
Sign up to request clarification or add additional context in comments.

3 Comments

im sorry its my mistake output just 24 25 26 27
@MehmetYüce by output you mean value of ip[1] item? Let me make question clear - you split line by /. That gives you ip array with second item having value like 24 25 26 27 and you want to convert this second item to array of integers?
@MehmetYüce OK, updated code - you should just use Split() without parameters or Split(' ')
2

If its really about IP-Adresses, this might help

class IPV4Adress
{
      public int BlockA {get; set;}
      public int BlockB {get; set;}
      public int BlockC {get; set;}
      public int BlockD {get; set;}

      public IPV4Adress(string input)
      {
           If(String.IsNullOrEmpty(input))
                throw new ArgumentException(input);

           int[] parts = input.Split(new char{',', '.'}).Select(Int32.Pase).ToArray();
           BlockA = parts[0];
           BlockB = parts[1];
           BlockC = parts[2];
           BlockD = parts[3];
      }

      public override ToString()
      {
           return String.Format("{0}.{1}.{2}.{3}",BlockA, BlockB, BlockC, BlockD);
      }        
}

Then read it from File:

IPV4Adress[] adresses = File.ReadLines(fileName).SelectMany(line=>line.Split('/')).Select(part => new IPV4Adress(part)).ToArray();

2 Comments

I am bored and thought, why not provide a fullblown soloution.
ups, I didn't see the comments sorry I thought how is this related with ip adresses but apparently it is:)
0

Given an array you can use the Array.ConvertAll method:

int[] myInts = Array.ConvertAll(arr, s => int.Parse(s));

(or)

int[] myInts = arr.Select(s => int.Parse(s)).ToArray();

(Or)

var converted = arr.Select(int.Parse)

A LINQ solution is similar, except you would need the extra ToArray call to get an array:

int[] myInts = arr.Select(int.Parse).ToArray();

Sourec

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.