Print Greetings

  • Add a new Java file in IntelliJ.

Create a new Java class file Greeting.java in src/main/java/starter folder.

Next, copy the following code to the Greeting.java file.

package starter;

import java.util.Scanner;

public class Greeting {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is your name?");
    String name = input.nextLine();
    System.out.println("Hello, " + name + "!");
  }
}

Finally, run the program!