0

How I can pass string argument that the user gives from the command Line to the main?

public static void main( String[] args )

for example user writes in the command line : string1 string2

How I can read the string2 and save it to a String variable to main?

javAndr
  • 43
  • 2
  • 5

1 Answers1

2

When you call the following

    main(String[] args)

you pass an array of strings as an argument to the main method

as such if you want to access any of those values passed in you would call args[index-you-want]

for example if you wanted the first string passed in you would call args[0]

to save to a string variable you would simply assign the value to a String variable

    String myString = args[0];

if you wanted String2

    String string2 = args[1];

Hope this helps.