Write a program that prompts the user to enter five names. You should store thes
ID: 3639956 • Letter: W
Question
Write a program that prompts the user to enter five names. You should store these names in an array or an arraylist. Once the fifth name is entered, the program will automatically print the results back to the user. This example shown here represents what the user types in green and what the program displays in red.Enter Name 1: Steve
Enter Name 2: John
Enter Name 3: Sally
Enter Name 4: Dave
Enter Name 5: Deborah
The names you entered are: Steve,John,Sally,Dave,Deborah
Important - you must use a loop to display the results.
Explanation / Answer
Code:
import java.util.Scanner;
class Names
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String[] name = new String[5];
int i;
for(i=0;i<5;i++)
{
System.out.print(" Enter Name "+ (i+1)+":");
name[i] = scan.next();
}
System.out.print(" The names you entered are: ");
for(i=0;i<5;i++)
{
System.out.print(name[i]);
if(i!=4) System.out.print(",");
else System.out.print(" ");
}
}
}