Answer :
Answer:
import java.util.Scanner;
public class HelloIDE
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
name = name.trim();
if(name.equals(""))
name = "stranger";
System.out.println("Hello, " + name + "!");
}
}
Explanation:
import the Scanner class to be able to get input from the user
Create an object of the Scanner class called input
Ask the user to enter the name and set it to the name
Trim the name using trim() method, removes the whitespaces from the string
Check the name. If name is equal to "", set the name as stranger. This way if the name is empty, or just contains whitespaces or if name is not given, name will be set as stranger.
Print the name in required format