Reputation: 123
I'm working on some homework and I need to get an input from the user which is a single line of numbers separated by spaces. I want to split this string and get the individual numbers out so that I can insert them into a Binary Search Tree.
I tried the split function and was able to rid of the white space but I'm not sure how to "collect" the individual numbers.
string data;
string[] newdata = { };
Console.WriteLine("Please enter a list of integers with spaces
between each number.\n");
data = Console.ReadLine();
newdata = data.Split(null);
Console.WriteLine(String.Join(Environment.NewLine, newdata));
I want to somehow collect the elements from newdata string array and convert them into integers but I'm having a tough time figuring out how to do that.
Upvotes: 3
Views: 5188
Reputation: 13676
Well, you could use Linq .Select
method combined with .Split
method:
List<int> newData = data.Split(' ').Select(int.Parse).ToList();
If you want user to be able to enter empty spaces we need to trim the resulting strings after split. For that we can use another overload of string.Split
method that accepts StringSplitOptions
:
List<int> newData = data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
Finally if you want to allow user to enter incorrect data at times and still get collection of valid ints you could use int.TryParse
and filter out values that were parsed incorrectly:
List<int> newData = data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => int.TryParse(s.Trim(), out var n) ? (int?)n : null)
.Where(x => x != null)
.Select(i => i.Value)
.ToList();
Upvotes: 5
Reputation: 18155
You can do as follows.
var numbers = Console.ReadLine();
var listOfNumbers = numbers.Split(new[]{" "},StringSplitOptions.RemoveEmptyEntries)
.Select(x=> Int32.Parse(x));
The above lines split the user input based on "whitespace", removing any empty entries in between, and then converts the string numbers to integers.
The StringSplitOptions.RemoveEmptyEntries
ensures that empty entries are removed. An example of empty entry would be an string where two delimiters occur next to each other. For example, "2 3 4 5"
, there are two whitespaces between 2 and 3,which means, when you are spliting the string with whitespace as delimiter, you end up with an empty element in array. This is eliminated by usage of StringSplitOptions.RemoveEmptyEntries
Depending on whether you are expecting Integers or Decimals, you can use Int32.Parse
or Double.Parse
(or float/decimal etc)
Furthermore, you can include checks to ensure you have a valid number, otherwise throw an exception. You can alter the query as follows.
var listOfNumbers = numbers.Split(new[]{" "},StringSplitOptions.RemoveEmptyEntries)
.Select(x=>
{
Console.WriteLine(x);
if(Int32.TryParse(x,out var number))
return number;
else
throw new Exception("Element is not a number");
});
This ensures all the element in the list are valid numbers, otherwise throw an exception.
Upvotes: 1
Reputation: 11389
Some smart LINQ answers are already provided, here is my extended step by step solution which also allows to ignore invalid numbers:
//Read input string
Console.Write("Input numbers separated by space: ");
string inputString = Console.ReadLine();
//Split by spaces
string[] splittedInput = inputString.Split(' ');
//Create a list to store all valid numbers
List<int> validNumbers = new List<int>();
//Iterate all splitted parts
foreach (string input in splittedInput)
{
//Try to parse the splitted part
if (int.TryParse(input, out int number) == true)
{
//Add the valid number
validNumbers.Add(number);
}
}
//Print all valid numbers
Console.WriteLine(string.Join(", ", validNumbers));
Upvotes: 3
Reputation: 23797
OK as code:
var words = data.Split();
int i;
List<int> integers = new List<int>();
foreach(var s in words)
{
if (int.TryParse(s, out i)) {integers.Add(i);}
}
// now you have a list of integers
// if using decimal, use decimal instead of integer
Upvotes: 3
Reputation: 75
Keep the spaces and do the "split" using the space "data.split(' ');".
Upvotes: 0