I am trying to read a line that consists of: 1 char and 2 integers.
My code looks like this:
char userHint = Convert.ToChar(Console.Read());
string[] v = Console.ReadLine().Split();
int a, b;
a = int.Parse(v[0]);
b = int.Parse(v[1]);
I recieve error System.FormatException: 'Input string was not in a correct format.'
.
Sample input char: 'O' Sample input ints: 1 2
As I understand, you want to split your string and then convert the characters to integer.
This code does what you try to do.
char userHint = Convert.ToChar(Console.ReadLine());
char[] v = Console.ReadLine().ToCharArray();
int a, b;
a = Int32.Parse(v[0].ToString());
b = Int32.Parse(v[1].ToString());
Console.WriteLine("a: "+ a);
Console.WriteLine("b: "+ b);
We use Console.ReadLine()
so when we press enter the program can wait for the next input
Instead of string[]
we use char[]
because we split the input using ToCharArray()
.
And then parse.
Input:
3
78
Output:
a: 7
b: 8
My problem can be solved by reading a char like so:
char userHint = Convert.ToChar(Console.ReadLine()[0]);
And then read the two integers:
string[] v = Console.ReadLine().Split();
int a, b;
a = int.Parse(v[0]);
b = int.Parse(v[1]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.