0

I am a beginner at java an I wanted to recreate something we did in school and alternate it a bit. I already know that public static void main(double[] args) lets you put in a number when you call on the method. But how do you name or use the number in the further course of your method? Thank you

Ellinchen
  • 3
  • 1
  • 1
    Does this answer your question? [Java Command line arguments](https://stackoverflow.com/questions/716153/java-command-line-arguments) – gioaudino Jan 24 '20 at 13:19

3 Answers3

0

The correct method is : public static void main(String[] args) . The 'args' array is the argument list of the file if you're running it from command-line e.g. "java HelloWorld 1 2 3" ( I think that IntelliJ have some options to do it directly from the IDE ). You can use later the arguments like this:

public static void main(String[] args) {
    String[] fileArguments = args;
    for (int i = 0; i < fileArguments.length; i++) {
        System.out.println(fileArguments[i]);
    }
}

PS : Your method signature is correct too, but the "main" method name is usually used for the String[] args parameter because in order to execute the program, JVM is looking for the public static void main(String[] args) method. ( the answer remains the same for the double[] type ).

user1234SI.
  • 1,812
  • 1
  • 8
  • 22
0

public (1) static void (2) main (3) (double[] (4) args (5)) means that you are calling a public (1) starting method called main (3) which will take an array of double numbers (4) called args (5) as input parameter and will return nothing (-> void) (2).

So if you want to access your input parameter (which is an array) you have to use args.

Then, since args is an array (so a collection of different elements), if want to access the first number you gave as input, you have to use args[0]. If you want to access the second, you have to use args[1], ecc.

System.out.println("The first number is " + args[0]) //prints the first number given as input
Maffe
  • 430
  • 6
  • 14
-1

public static void main(String[] args) in this args is an array which can take in values as a string

For example: java MyClass 10 23 34

So all the standard array methods can be used on this array.

args.method_name();

nsm10
  • 29
  • 4
  • Have you tried the "java MyClass 10 23 34" command in the command-line with **main(double[])** method signature? The JVM is looking for **main(String[])** method when you try to execute the program and no IDE lets you execute the program without a `public static void main(String[] args)`. – user1234SI. Jan 24 '20 at 13:43
  • Ohh apologies. I didn't know that. So all arguments can only be taken as string. Cool !! – nsm10 Jan 25 '20 at 07:30