118k views
1 vote
Assume that name has been declared suitably for storing names (like "Misha", "Emily" and "Sofia"). Assume also that stdin is a variable that references a Scanner object associated with standard input Write some code that reads a value into name then prints the message "Greetings, NAME" on a line by itself, where NAME is replaced the value that was read into name. For example, if your code read in "Rachel" it would print out "Greetings, Rachel" on a line by itself.

User Fbonetti
by
5.9k points

1 Answer

7 votes

Answer:

// program in java.

import java.util.*;

// class definition

class Main

{// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// object to read input

Scanner scr=new Scanner(System.in);

// ask to enter name

System.out.print("Enter Your name: ");

// read name from user

String NAME=scr.nextLine();

// print message

System.out.println("Greetings,"+NAME);

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Read name from user with the help of scanner object and assign it to variable "NAME".Then print a Greetings message as "Greetings,NAME" where NAME will be replaced with user's input name.

Output:

Enter Your name: Rachel

Greetings,Rachel

User Kyanny
by
5.8k points