1

so I'm new to using IntelliJ and I've tried googling but to no avail.

I'm creating a simple java program that basically prints hello and gets the user input (name) and prints it... Just to get the ball rolling. Normal Hello World prints fine..

But as soon as I add any [args] in it just crashes? Is there a way I can type the input in?

public class Main {

    public static void main(String[] args) {

        System.out.println("Hello, " + args[0] + "!");
    }
}
Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
Javanoob33
  • 89
  • 3
  • 12

3 Answers3

2

You need to provide at least 1 argument if you access args[0] otherwise you get ArrayIndexOutOfBoundsException.

Why ? because the args[] is empty without any arguments passed so accessing the first one will throw the exception

How do you input commandline argument in IntelliJ IDEA?

Community
  • 1
  • 1
Cris
  • 4,947
  • 6
  • 44
  • 73
1

There's an "edit configurations" item on the Run menu. In that panel, you can create a configuration and then you can choose the Class containing main().

add VM parameters and command-line args, specify the working directory and any environment variables.

you are done.

Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
0

Sorry guys figured it out:

Go to Run Edit Configurations > on the left side make sure you're in your Main class or whatever class you're using

Enter what you want in the program arguments. i.e. "James"

Javanoob33
  • 89
  • 3
  • 12