Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're trying to detect command line arguments then I would consider something like this approach: </p> <p>First you'll want to make sure the user actually inputs some amount of arguments:</p> <pre><code>public static void main(String[] args) { if(args.length &gt; 0) { //Check each set of arguments. } else { System.out.println("Invalid number of arguments"); System.exit(1); // Here you can either do a try-catch and throw an exception or just simply exit if the user doesn't input the `enter code here`correct number of arguments. } } </code></pre> <p>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.</p> <p>Let's say you have the user to use the following method to input the parameters:</p> <pre><code>[program] A=2 B=3 C=A+B </code></pre> <p>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:</p> <pre><code>if(args[i].toUpperCase().startsWith("A")) { resultA = args[i].split("="); //Split by the delimter "=" a = Double.parseDouble(resultA[1]); } </code></pre> <p>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.</p> <p>We will simply check the length of args, and iterate through to find the parameters values:</p> <pre><code>public static void main(String[] args) { double a = 0; double b = 0; double c = 0; String[] resultA = null; String[] resultB = null; String[] resultC = null; String[] result = null; if(args.length &gt; 0) { for(int i=0; i &lt; args.length; i++) { if(args[i].toUpperCase().startsWith("A")) // Implemented startsWith() thanks to WChargin { resultA = args[i].split("="); //Split by the delimter "=" a = Double.parseDouble(resultA[1]); } else if(args[i].toUpperCase().startsWith("B")) { resultB = args[i].split("="); b = Double.parseDouble(resultB[1]); } else if(args[i].toUpperCase().startsWith("C")) { result = args[i].split("="); //We don't need C, split all of the arguments resultC = result[1].split(""); //This way we have an array of strings for each value to iterate through // The only problem with split("") is that we will get an empty string at the beginning of the array for(int j=1; j &lt; resultC.length; j++) { if(resultC[j].toUpperCase().startsWith("A")) { if(resultC[j+1].equals("+")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = a + a; break; // Once we get out answer, break otherwise we'll get a ArrayIndexOutOfBoundsException because the program will continue iterating } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = a + b; break; } else { System.out.println("Argument parse error"); } } else if(resultC[j+1].equals("-")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = a - a; break; } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = a - b; break; } else { System.out.println("Argument parse error"); } } else if(resultC[j+1].equals("*")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = a * a; break; } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = a * b; break; } else { System.out.println("Argument parse error"); } } else if(resultC[j+1].equals("/")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = a / a; break; } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = a / b; } else { System.out.println("Argument parse error"); } } } else if(resultC[j].toUpperCase().startsWith("B")) { if(resultC[j+1].equals("+")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = b + a; break; } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = b + b; break; } else { System.out.println("Argument parse error"); } } else if(resultC[j+1].equals("-")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = b - a; break; } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = b - b; break; } else { System.out.println("Argument parse error"); } } else if(resultC[j+1].equals("*")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = b * a; break; } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = b * b; break; } else { System.out.println("Argument parse error"); } } else if(resultC[j+1].equals("/")) { if(resultC[j+2].toUpperCase().startsWith("A")) { c = b / a; break; } else if(resultC[j+2].toUpperCase().startsWith("B")) { c = b / b; break; } else { System.out.println("Argument parse error"); } } } else { System.out.println("Argument error in C"); System.exit(1); } } } } } else { System.out.println("Invalid number of arguments"); System.exit(1); } System.out.printf("A: %f\nB: %f\nC: %f\n", a, b, c); } </code></pre> <p>Please note I probably did not account for all possibilities.</p> <p>There are definitely easier ways to parse through command line arguments. I am giving you the overly long method.</p> <p>I hope this helps you! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload