Write a Java program to print your name with greetings Hello, [Your name]!

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Solution

import java.util.Scanner;
public class ClassName {
public static void main(String[] args) 
{
String name;		
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name here ");
name = sc.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Output
Enter your name here Daniel
Hello, Daniel!

Explanation

  • The main method is the entry point of the Java code.
  • A variable “name” is declared of type “String” which is used to store the name entered by the user.
  • Scanner class is used to read input from the console. The object “sc” of the Scanner class reads the entire line of input entered by the user using the nextLine() method.
QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.