So here is my assignment:

Write a java program to read a name from user .
The name is typed all in one line.
The name may include 3 or 2 or 1 part from:
firstname
middlename
lastname

You should create an email id from the name and print it.
the email is made of firstletter of firstname followed by up to 7 characters of lastname and @gpc.edu

Don't use printf method.
Even if the name contains mixed case, your email should be all in lowercase.

Example input : John Doe
Example output: jdoe@gpc.edu


Example input : John M. Doe
Example output: jdoe@gpc.edu

Example input : John Doe Smith
Example output: jsmith@gpc.edu

Example input: George Washington
Example output: gwashing@gpc.edu

I'll be appreciate with your help.

Recommended Answers

All 3 Replies

read it using an instance of the Scanner class. you read the entire line as one single String.
next, use the split() method to split that String to the separate names.
after which, paste them together
in the order you see above, you can use charAt(), substring,.. and simple String concatenation

So here is my assignment:

Write a java program to read a name from user .
The name is typed all in one line.
The name may include 3 or 2 or 1 part from:
firstname
middlename
lastname

You should create an email id from the name and print it.
the email is made of firstletter of firstname followed by up to 7 characters of lastname and @gpc.edu

Don't use printf method.
Even if the name contains mixed case, your email should be all in lowercase.

Example input : John Doe
Example output: jdoe@gpc.edu


Example input : John M. Doe
Example output: jdoe@gpc.edu

Example input : John Doe Smith
Example output: jsmith@gpc.edu

Example input: George Washington
Example output: gwashing@gpc.edu

I'll be appreciate with your help.

well lets see first thing how to accept input through the console? you can use the scanner class of java:

Scanner sc = new Scanner(System.in);//initiliaze scanner to get user input
        System.out.println("Please enter your full name i. John Damien Doe: ");//prompt for full name
        String fullname = sc.nextLine();//read in the full name

now we have the name how do we see if the entered 1,2 or 3 names? well we could take a look at seperating the name by the spaces, because John would have no spaces showing only one name was enetered ... John Damien would have 1 space showing only two name swere entered and ofcouse now with three names would have 2 spaces John Damien Doe. for that you might want to look at the split() method which will split a string using a certain string i.e:

String[] names=fullname.split(" ");

now the resultin you string array will either be a length of 3,2 or 1 depending on how many spaces there were... from there we get the length of the array and know how many names were inputed... next we get the first name of the array and use java substring() method to get the first letter or the charAt() method to get the first character of the name... form there we go to the last index of the array and check its sixe ... if its over 7 then we use the substring method again to only make it use up to 7 letters of the name, if its less then 7 though we use the entire name... hope its enough to get you going

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.