954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Taking two numbers from a string

Hello everyone, I'm coming up with a very primitive question:
I need to take two numbers from console and process them. Since I'm getting input from console with Console.Readline(), how can I seperate those numbers from the string?
Thanks in advance..

newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Are the numbers entered on the same line? If so, what are you delimiting the numbers with?

If it's on 2 seperate lines of entry:

double myNumber = 0;
if (double.TryParse(myString, out myNumber) == true)
{
   //The cast worked, myNumber now contains a double parsed from the string
}
else
{
  //There was a problem with the parsing, perhaps tell the user?
}
skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

Thanks a lot,
Numbers are entered on the same line, and they don't have to be big, like between 0 and 10000.

[
QUOTE=skatamatic;1736998]Are the numbers entered on the same line? If so, what are you delimiting the numbers with?

If it's on 2 seperate lines of entry:

double myNumber = 0;
if (double.TryParse(myString, out myNumber) == true)
{
   //The cast worked, myNumber now contains a double parsed from the string
}
else
{
  //There was a problem with the parsing, perhaps tell the user?
}


[/QUOTE]

newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Ok. Are they seperated by a space, comma, EOL character?

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

just a space..

newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Ok. Then to parse this:

double myNumber1 = 0;
double myNumber2 = 0;
string[] myStrings = String.Split(myInputString, ' ');
if (double.TryParse(myStrings[0], out myNumber1) == true && double.TryParse(myStrings[1], out myNumber2) == true)
{
    //Both numbers parsed successfully
}
else
{
   //one or more of the numbers didn't parse properly
}
skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

Thank you very much, it is very helpful! Thanks..

newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You