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] + "!"); } } 
0

4 Answers 4

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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"

Comments

0

args[] does not contain any elements. Therefore, when you try to access the first element, it throws an ArrayIndexOutOfBoundsException because there are no elements in the array to access and any index passed in would be out of bounds. I would recommend doing a general Google search on what the main method is in Java to get a better understanding of the reasoning behind this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.