I was thinking about this and wondering if it is possible for a string and an integer to be on the same input line. For example, the user puts in “A=2”, “B=3”, and “C= A+B”, with C equaling 5. If so, what type of technique would I have to know? What would I have to look up?
Share
If you’re trying to detect command line arguments then I would consider something like this approach:
First you’ll want to make sure the user actually inputs some amount of arguments:
The tricky part will be determining whether the user has input A or B or C, and that will cause some amount of parsing. But you will need to know where in the input String it lies, either by telling the user the usage format, or searching the string.
Let’s say you have the user to use the following method to input the parameters:
Since WChargin pointed out that args is space-delimted, which slipped my mind, I decided to break up each set of arguments into their own string array. For A and B I split the string by the delimiter by the character “=” as so:
Which for A and B will produce the array {A,2}, {B,3}. C I will split twice. First by the character “=” which will produce {C,A+B} and then split each string, which will produce { ,A,+,B}. Note that split() produces an empty string in resultC[0], so we start iterating at 1.
We will simply check the length of args, and iterate through to find the parameters values:
Please note I probably did not account for all possibilities.
There are definitely easier ways to parse through command line arguments. I am giving you the overly long method.
I hope this helps you!