0

How i can get input from user where the two inputs are separated by space? I tried doing this with Conver.ToInt32(Console.ReadLine());But it is showing format exception. Please tell whether there is some other way of getting input from user. Please help! Thank you for reading my problem.

Ex: input: 10 2 input is in the above format. The value 10 & 2 should be stored in different variables.

  • 2
    Read input in as a string and split it with space. – Pankaj Nema Sep 04 '14 at 11:25
  • I presume the problem isn't getting the input from the user but parsing it into two variables? – Chris Sep 04 '14 at 11:26
  • possible duplicate of [C# Split A String By Another String](http://stackoverflow.com/questions/2245442/c-sharp-split-a-string-by-another-string) – DMAN Sep 04 '14 at 11:26

4 Answers4

1
string[] inputs =TextBoX1.Text.Split(" ");
String first = inputs[0].ToString();
String second = inputs[1].ToString();
ateenraw
  • 61
  • 2
  • 12
  • Can you expand your answer to include an explanation of your code? It helps the reader more than you might think. – gunr2171 Sep 04 '14 at 18:26
0
string[] inputs =Console.ReadLine().Split(null);

int input1=TryParse(inputs[0]);
int input2=TryParse(inputs[1]);
apomene
  • 14,282
  • 9
  • 46
  • 72
0

Console.ReadLine() will only return values once the user enters a new-line (IE. presses return).

To get what you're after, you might consider using Console.ReadKey() or Console.Read() in order to get individual characters.

Such that your code would look something like:

public int GetNextNumber()
{
    char nextChar;
    string numberString = string.Empty;
    while (!char.IsWhiteSpace(nextChar = (char)Console.Read())
        numberString += nextChar;
    int result;
    if (!int.TryParse(numberString, out result))
        throw new InvalidCastException("Specified string is not an integral");
    return result;
}

You can then use that method to read each individual number from the console, as the user enters it. You could also do a Console.ReadLine() and split the resulting string to get the numbers like some of the others suggested

Hope this helps.

t0yk4t
  • 854
  • 8
  • 10
0

Because I do not see the actual code you tried I can just assume you did something like:

int input = Convert.ToInt32(Console.ReadLine());

and the actual input was "10 2", which is a string. The error occured when the method tried to convert the empty space to an int.

You need to save the input as a string variable, then split the string variable with empty spaces as seperator. You can save the result as an string array (string[]) and then convert each array element of the string[] to an int.

Reading the input for your code:

string input = Console.Readline();

Split method for your code:

string[] stringArray= input.Split(' ');

Convert each string to an int for your code:

List<int> integerList = new List<int>();

foreach (string str in array)
{
    integerList.Add(Convert.ToInt32(str));
}

Note, that this would also work for an input with more than 2 numbers, for example "10 2 17 3 19 27" and you do not need an own variable for each input part because you add each value to a list. Why a list ? because the size is dynamic. Your problem would also work with an array instead of a list because at the moment you split your string you know the size the intArray would need but a list is more comfortable.

Paul Weiland
  • 727
  • 10
  • 24