In: Computer Science
Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of email addresses where the address is of the following form: [email protected].
please help!!!!
Hi,
Hope you are doing fine. I have writrten the code for the above question in java. The logic is pretty straight forward and the clear explanation is provided using the comment sthat have been highlighted in bold. Also have a look at the code snippet below the program to get a clear picture of the indentation of the code.
Program:
import java.util.Scanner;
public class Name {
public static void main(String[] args) {
// TODO Auto-generated method
stub
//array to store first
names
String [] first =new
String[100];
//array to store last
names
String [] last =new
String[100];
//sentinel variable of type
String which is assigned to 'y' initially
String sentinel="y";
//to traverse through the
index of array
int i=0;
//creating new scanner for
input from user
Scanner sc=new
Scanner(System.in);
System.out.println("Enter first and
last names of users\n");
//while sentinel is not equal to
'n' the loop continues
while(!sentinel.equals("n"))
{
System.out.println("Enter first name: ");
//Storing the first name entered by user in
first[i]
first[i]=sc.next();
System.out.println("Enter last name: ");
//Storing the last name entered by user in
last[i]
last[i]=sc.next();
System.out.println("Enter any key to continue and 'n' to
stop");
//Taking
sentinel input
sentinel=sc.next();
//incrementing i value to next index
i++;
}
//Printing empty
line
System.out.println();
System.out.println("List of emails:
");
//this for loop traverses
the two arrays first and last. Maximum size of both arrays is
i
for(int j=0;j<i;j++)
//Printing output as per question. '+' is used to
concatenate two strings
System.out.println(first[j]+"."+last[j]+"@mycollege.edu");
}
}
Executable code snippet:
Output: