Challenge: Split an Email
Completed
Read an email address and print the user name and the domain on separate lines, then whether the domain ends with .com.
Input
One email address containing one @.
Output
User: <before @> Domain: <after @> Dot com: <true/false>
Example
Input
asha@vulntech.com
Output
User: asha Domain: vulntech.com Dot com: true
Editor
Java
Hints
indexOf("@") finds the position of @.
substring(0, at) is everything before it, substring(at + 1) everything after.
endsWith(".com") gives true or false.
Solution explanation
Find the @ once, then cut the string into the part before and after it.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String email = sc.nextLine().trim();
int at = email.indexOf("@");
String domain = email.substring(at + 1);
System.out.println("User: " + email.substring(0, at));
System.out.println("Domain: " + domain);
System.out.println("Dot com: " + domain.endsWith(".com"));
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module