Jump to content

How to eficiently input 2 separate numbers in 1 line in C#

Vuk
1 minute ago, Vuk said:

Title

Title:

Quote

How to eficiently input 2 separate numbers in 1 line in C#

Do you mean by 'input', 'have the user input something'? Or do you mean initialize a variable?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Could you define "efficient" a bit more? Efficient in terms of speed entering that number(s) or processing the input?

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Eigenvektor said:

Could you define "efficient" a bit more? Efficient in terms of speed entering that number(s) or processing the input?

Efficient in the means of not getting a WriteLine string then doing s[0] and s[1] for 2 char numbers and then parsing them...

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, minibois said:

Title:

Do you mean by 'input', 'have the user input something'? Or do you mean initialize a variable?

User input in console

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Vuk said:

Efficient in the means of not getting a WriteLine string then doing s[0] and s[1] for 2 char numbers and then parsing them...

Actually s[0] and s[2] sorry. And oof you'd need to convert the gotten chars to strings and then to int bc int.Parse don't work with chars for some reason.

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Vuk said:

Actually s[0] and s[2] sorry. And oof you'd need to convert the gotten chars to strings and then to int bc int.Parse don't work with chars for some reason.

You'll want to read a string, split it (eg on space) and then parse both strings into a number.

 

I would worry more about making that convenient for the user rather than efficient. Unless you're parsing thousands of numbers you don't need to worry about speed.

 

~edit: By thousands of numbers I mean from a source other than user input. A user will take far more time typing than the computer spends processing the input.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, Vuk said:

How to eficiently input 2 separate numbers in 1 line in C#.

User input in console

So you've begun to brush up against lexical analysis.

The first step is to define a language that the user will use to input multiple numbers, say:

<UserNumber> := <number> <delimiter> <UserNumber> | <number> <delimiter> <number>;
	<number> := [0-9,]+;
    <delimiter> := " ";


Then, reading this language description gives you all the information you need to know how to extract an n length list of numbers from a user string:

First, we read in the user string. With this simple a definition, any string that only contains characters in the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ',', \s} and has no substrings "\s,\s" is a valid <UserNumber>.

We then split the string at the '\s' character, and remove any empty strings that return.

We then remove all of the commas from the remaining strings in the collection.

We then convert the collection of strings into a collection of ints by calling Convert.ToInt32(string s) on all of the strings in the collection.

That's just one possible way to do it, but on the lines of a good one. Thinking about user input as a language gives you the ability to systematically derive systems that can understand user input, yielding better and more maintainable code. It also gives you the ability to handle erroneous input gracefully instead of failing or entering into areas of undefined behavior.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×