java - Validating the amount of command line arguments -


I'm trying to validate that there are only three arguments passed in this program I use two line integers I'm trying to pass that looks like 23 23+. But I want an error, if they debate less than three or more than three logic as it is now, this error message gives a message when there are more than three arguments but less than three, no help will be great. Public class arithmetic {public static zero main (string [] args) {// variable first set number and second number int firstNumber = Integer.parseInt (args [0]); Int secondNumber = integer Paracet (Argos [1]); String arithmetic = args [2]; Int length = args.length; If (length! = 3) {System.out.println ("You seem to enter an int, int then, +, - sign an operation like x or /."); Return; } If (arithmetic.quals ("+") {int addition = firstNumber + secondNumber; System.out.println (Args [0] + "+ + Arg + 2 + + + + Args [1] +" = "+ Extra); Int total = String.valueOf (extra) .length (); System.out.println (additional + "length" + total) is; } And if (arithmetic. Equals ("-")) {int minus = firstNumber - secondNumber; System.out.println (args [0] + "+ + arg [2] + +" + args [1] + "=" + zero); Int total = string.valouf (zero). Length (); System.out.println (zero + "length" + total) is; } And if (arithmetic.quilues ("/")) {int division = firstNumber / secondNumber; System.out.println (args [0] + "+ + args [2] +" + + args [1] + "=" + partition); Int total = String.valueOf (Partition). Length (); System.out.println (partition + "length" + total) is; } And if (arithmetic. Equals ("x")} {int multiply = firstNumber * secondNumber; System.out.println (Args [0] + "+ + Argus [2] + +" + Argus [1] + "=" + times); Int total = String.valueOf (multiplied) Length (); System.out.println (multiplication + "length of +" + total) is; } // The following argument argates that the length of each argument is System.out.println (args [0] + "length" + args [0]. Length ()); System.out.println (args [1] + "length" + args [1] is the length ()); System.out.println ("the logic passed" + RG [0] + "+ + Argus [1] +" + + Argus [2]); }}

You have code lines that assume that you have at least 3 Logic is to check how many arguments are there. Move these lines:

  // Set the variable first number and the second number is int firstNumber = Integer.parseInt (args [0]); Int secondNumber = integer Paracet (Argos [1]); String arithmetic = args [2]; Otherwise, if you enter at least three command line arguments below your "! = 3" probe, then how many arguments do you have before going to one of those rows < code> ArrayIndexOutOfBoundsException .   

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -