+ 3

How can I read argument in java

26th Aug 2018, 7:04 PM
הראל ברודאי
הראל ברודאי - avatar
4 Answers
+ 2
But I don't can just use the command for example Int a = args [0] ;?
26th Aug 2018, 7:15 PM
הראל ברודאי
הראל ברודאי - avatar
+ 2
הראל ברודאי Sure, you might pass arguments to your program in the beginning of execution, not runtime. Your main function has a String array as a parameter for this purpose. When executing the program with terminal/cmd, arguments are stored in 'args' array. For example 'java MyProgramsName 1 2 text' causes that args[0] will store 1, args[1] will store 2, args[2] will store "text". Then you can assign these values to your variables. But remember that all the data passed to args[] is parsed to a String.
27th Aug 2018, 12:16 AM
Steppenwolf
Steppenwolf - avatar
+ 1
Scanner scan = new Scanner(System.in); int number = scan.nextInt(); // for integers String phrase = scan.nextLine(); // for strings double number2 = scan.nextDouble(); // for doubles etc.
26th Aug 2018, 7:11 PM
Steppenwolf
Steppenwolf - avatar
+ 1
Thanks
26th Aug 2018, 7:13 PM
הראל ברודאי
הראל ברודאי - avatar