input - JAVA Command line arguments to get info -


i have small question cant understand , hope can please . want write program info program using command line, example (java xx 10 20). in program got this

   int coffeecups= integer.parseint(args[0]);    int coffeeshots= integer.parseint(args[1]);      if (args.length==0)         {          system.out.print ("no arguments..");                     system.exit(0);         }         else if (args.length==1)         {system.out.println("not enough arg..");                     system.exit(0);      }         else if (args.length>2)         {system.out.println("too many arg.");                     system.exit(0);      }         else if (integer.parseint(args[0]<0) && integer.oarseint(args[1]<0)         {system.out.println("negative chain arg");         system.exit(0); }                     else if (integer.parseint(args[0]<0) || integer.oarseint(args[1]<0)         {system.out.println("negative  arg");         system.exit(0);} 

i want enter 2 positive integers command line.. otherwise should reject inputs, thing sometime came en error (exception in thread "main" java.lang.arrayindexoutofboundsexception: 0) , program runs without entering 2 integers in command line... gotta finish code possible, , i'de appreciate ur p.s. dont worry identation program not done yet

first of all, may want use command-line-arguments parsing facility.

you're trying access indices not exist:

// said there first argument? int coffeecups = integer.parseint(args[0]); // said there second argument? int coffeeshots = integer.parseint(args[1]); 

you need first check, access:

// using sentinel value. if you're not familiar // shortend `if` see notes. int coffeecups = args.length > 1 ? integer.parseint(args[0]) : null; int coffeeshots = args.length > 2 ? integer.parseint(args[1]) : null;   if (coffeecups == null || coffeeshots == null){     throw new exception("not enough arguments"); }   if (args.length > 2){     throw new exception("too many arguments"); } 

there case in arguments not integers. numberformatexception if that's case...

notes:

short if notation (x ? y : z) used return y in case x true, otherwise returns z.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -