2014-04-21 6 views
0

J'essaye d'obtenir une chaîne de la console et de mettre tous les éléments dans un tableau int. Il se produit une erreur indiquant que mon entrée était dans un format incorrect. J'essaie avec "1 1 3 1 2 2 0 0" et j'ai besoin de ceux-ci comme valeurs int et plus tard effectuer quelques calculs avec eux.String array to Int array

Voici ma tentative:

class Program 
{ 
    static void Main() 
    { 

    string first = Console.ReadLine(); 
    string[] First = new string[first.Length]; 

    for (int i = 0; i < first.Length; i++) 
    { 
     First[i] += first[i]; 
    } 

    int[] Arr = new int[First.Length];//int array for string console values 
    for (int i = 0; i < First.Length; i++)//goes true all elements and converts them into Int32 
    { 
     Arr[i] = Convert.ToInt32(First[i].ToString()); 
    } 
    for (int i = 0; i < Arr.Length; i++)//print array to see what happened 
    { 
     Console.WriteLine(Arr[i]); 
    } 
} 
} 

Répondre

2

ici, vous allez:

class Program 
{ 
    static void Main() 
    { 
     string numberStr = Console.ReadLine(); // "1 2 3 1 2 3 1 2 ...." 
     string[] splitted = numberStr.Split(' '); 
     int[] nums = new int[splitted.Length]; 

     for(int i = 0 ; i < splitted.Length ; i++) 
     { 
     nums[i] = int.Parse(splitted[i]); 
     } 
    } 
} 
3

essayer cette

string str = "1 1 3 1 2 2 0 0"; 
int[] array = str.Split(' ').Select(int.Parse).ToArray(); 

Demo

5

Vous devez utiliser String.Split méthode pour diviser votre chaîne avec l'espace ' ' dans un tableau de chaînes, puis Convertit chaque élément en nombre entier. Vous pouvez itérer tableau de chaînes en utilisant System.Linq de manière efficace

using System.Linq; //You need add reference to namespace 

static void Main() 
{ 
    string numbersStr = "1 1 3 1 2 2 0 0"; 
    int[] numbersArrary = numbersStr.Split(' ').Select(n => Convert.ToInt32(n)).ToArray(); 
} 

DEMO

1

Essayez de changer à ceci:

string s = Console.ReadLine(); 
string[] arr = s.Split(' '); //Split the single string into multiple strings using space as delimiter 
int[] intarr = new int[arr.Length]; 

for(int i=0;i<arr.Length;i++) 
{ 
    intarr[i] = int.Parse(arr[i]); //Parse the string as integers and fill the integer array 
} 

for(int i=0;i<arr.Length;i++) 
{ 
    Console.Write(intarr[i]); 
} 
1

Vous n'êtes pas diviser la chaîne en utilisant delimiter espace.

 string first = Console.ReadLine(); 
     int len = first.Split(new [] 
         {' '},StringSplitOptions.RemoveEmptyEntries).Length; 
     string[] First = new string[len]; 

     for (int i = 0; i < len; i++) 
     { 
      First[i] = first.Split(' ')[i]; 
     } 

     int[] Arr = new int[First.Length];//int array for string console values 
     for (int i = 0; i < First.Length; i++)//goes true all elements and converts them into Int32 
     { 
      Arr[i] = Convert.ToInt32(First[i].ToString()); 
     } 
     for (int i = 0; i < Arr.Length; i++)//print array to see what happened 
     { 
      Console.WriteLine(Arr[i]); 
     } 
1

Vous ne pouvez pas essayer avec "1 1 3 1 2 2 0 0", car il tente d'analyser les espaces entre les numéros. Si vous voulez que votre programme fonctionne, vous devez soit faire votre chaîne d'entrée comme ça: "11312200" ou vous pouvez faire un tableau char ou juste un seul caractère si vous n'avez pas plus d'un séparateur où vous affectez puis .split la chaîne en passant le séparateur, comme ça:

string Numbers = "1 1 3 1"; 

string[] seperatedNumbers = Numbers.Split(' '); 

// perform your following actions 
+0

vous devez assigner à une variable 'String []' dans la deuxième ligne. –