+ 3
How can I read argument in java
4 odpowiedzi
+ 2
But I don't can just use the command for example Int a = args [0] ;?
+ 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.
+ 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.
+ 1
Thanks