java - How do I make Double.parseDouble(args[0]); work? -


i want write little program should make simple calculation while taking input 1 variable. here's code:

public class dfact {     public static void main(string[] args) {         int n;         int df;         int i;          n = integer.parseint(args[0]);           df = 1;                          = n;                        while(i > 0) {             df = df * i;                     = - 2;                   }          system.out.println(df);       } } 

for programm getting following message:

exception in thread "main" java.lang.arrayindexoutofboundsexception: 0
@ dfact.main(dfact.java:9)

i recommend change code this:

public class dfact {     public static void main(string[] args) {         int n;         int df;         int i;          (string arg : args) { // prevent nullpointers , index out of bound             n = integer.parseint(arg);             df = 1;             = n;              while (i > 0) {                 df = df * i;                 = - 2;             }              system.out.println(df);         }     } } 

then open command line in directory of file , type:

javac {filename.java}  

and

java {filename} 1 2 3 4 5 6  

Comments

Popular posts from this blog

meteor - inserting data to database gives error "insert failed: Method '/texts/insert' not found" -

angular - DownloadURL return null in below code -