Reputation: 711
Hello friends i am a total beginner in c#. I want to read numbers in the following format
2
1 10
3 5
For reading 2 i have used Convert.ToInt32(Console.ReadLine())
method and successfully stored it. But for the next input i want to read the number and after a space i want to read another number. I cannot use ReadLine() method.. I have used Convert.ToInt32(Console.Read())
But the number 1 is read as 49
So How do i achieve reading numbers
In java i have found a similar class called Scanner which contains methods line nextInt()
Is there any C# equivalent to that class.
In short i just want to know how to read and store numbers(integers or floats) in C#.
Upvotes: 0
Views: 1386
Reputation: 29194
I've been doing this on HackerRank a lot, here's a helper method I use for reading an array of integers:
static int [] StringToIntArray(string s) {
string [] parts = s.Split(' ');
int [] arr = new int[parts.Length];
for (int i = 0; i < arr.Length; i++) {
arr[i] = Convert.ToInt32(parts[i]);
}
return arr;
}
Here's a sample reading two numbers on one line, then two lines with those many numbers into arrays:
string [] parts = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(parts[0]);
int m = Convert.ToInt32(parts[1]);
int [] a = StringToIntArray(Console.ReadLine());
int [] b = StringToIntArray(Console.ReadLine());
Sample input:
3 5
40 50 60
10 20 30 100 7500
Upvotes: 0
Reputation: 15076
You problem is about converting strings into integers, as Console.Readline always returns a string.
There are many ways to parse multiple ints, but you will basically need to split the string using some method. I don't know of a Scanner
in C# (but I guess you should seach for some kind of tokenizer to find it, since this would be the standard name.
Writing one is not that hard, especially if you only expect integers to be separated by spaces. Implementing it in a method could be something like
// Optional seperators
public IEnumerable<int> ParseInts(string integers, params char[] separators)
{
foreach (var intString in integers.Split(separators))
{
yield return int.Parse(intString);
}
}
public IEnumerable<int> ParseInts(string integers)
{
return ParseInts(integers, ' ');
}
Upvotes: 1
Reputation: 35822
Use this
string[] lines = File.ReadAllLines("Path to your file");
foreach (string line in lines)
{
if (line.Trim() == "")
{
continue;
}
string[] numbers = line.Trim().Split(' ');
foreach (var item in numbers)
{
int number;
if (int.TryParse(item, out number))
{
// you have your number here;
}
}
}
Upvotes: 0
Reputation: 124632
You can use ReadLine()
and just split on whitespace, i.e.,
void Main( string[] args )
{
var numbers = new List<int>();
while(whatever)
{
var input = Console.ReadLine();
var lines = input.Split(
new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries
);
foreach( var line in lines )
{
int result;
if( !int.TryParse( line, out result ) )
{
// error handling
continue;
}
numbers.Add( result );
}
}
}
Upvotes: 4
Reputation: 35477
You can use the Split method, to break the line into numbers.
while (true)
{
var s = Console.ReadLine();
if (s == null) break;
foreach (var token in s.Split(' '))
{
int myNumber = int.Parse(token);
// ... do something ....
}
}
Upvotes: 2