Write a java program to find username from a valid email string.

Write a program to read a valid email id and extract the username from the email Id. In this article, we will see the following two approach to solve a given problem.

  • By using StringTokenizer class.
  • By using the subString() method.

Note – username is the string appearing before @ symbol.

Input and Output Format

  • Input consists of a string.
  • The output consists of a string.

Refer sample output for formatting specifications

Sample Input 1:
admin@xyz.com

Sample Output 1:
admin

Program to find username from a valid email string in java

Following are the steps to find a username from a valid email string:

  • Input email Id from the user.
  • Pass it to the getvalues() method.
  • Inside the method, First, use the string tokenizer to break the string into tokens by separating it with the ‘@’ symbol.
  • Now, just get the first token by calling the nextToken() method and store it in variable s2.
  • At last, print the value in variable s2.
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    String s1 = sc.nextLine();
    getvalues(s1);
  }

  public static void getvalues(String s1) {

    StringTokenizer st = new StringTokenizer(s1, "@");
    String s2 = st.nextToken();
    System.out.println(s2);

  }
}

Output

Extract username from a valid email string using subString()

Following are the steps to find a username from a valid email string:

  • Input email Id from the user.
  • Pass it to the getvalues() method.
  • Inside the method, First, use the subString() method. Here, the substring begins from both 0th index and extends to the index of character  “@” store it in variable s2.
  • At last, print the value in variable s2.
package com.demo2;

import java.util.Scanner;
import java.util.StringTokenizer;

public class FindUserName {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        getvalues(s1);
    }

    public static void getvalues(String s1) {
        String s2 = s1.substring(0,s1.indexOf("@"));
        System.out.println(s2);
    }
}

Output

Get Domain Name from the Email Id in Java

Write a program to read a valid email id and extract the domain name from the email Id. Here, we will see the following two approach to solve a given problem.

  • By using StringTokenizer class.
  • By using the subString() method.

Input and Output Format

  • Input consists of a string.
  • The output consists of a string.

Refer sample output for formatting specifications

Sample Input 1:
admin@xyz.com

Sample Output 1:
xyz.com

Using StringTokenizer Class:

  • Input email Id from the user.
  • Pass it to the getvalues() method.
  • Inside the method, First, use the string tokenizer to break the string into tokens by separating it with the ‘@’ symbol.
  • Now, just get the second token by calling the nextToken() method and store it in variable s2.
package com.demo2;

import java.util.Scanner;
import java.util.StringTokenizer;

public class FindUserName {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        getvalues(s1);
    }

    public static void getvalues(String s1) {
        StringTokenizer st = new StringTokenizer(s1, "@");
        st.nextToken();
        String s2 = st.nextToken();
        System.out.println(s2);
    }
}

Output

Using subString() method:

  • Input email Id from the user.
  • Pass it to the getvalues() method.
  • Inside the method, First, use the subString() method. Here, the substring begins from the index of the “@” +1 symbol and stores it in variable s2.
  • At last, print the value in variable s2.
package com.demo2;

import java.util.Scanner;
import java.util.StringTokenizer;

public class FindUserName {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        getvalues(s1);
    }

    public static void getvalues(String s1) {
        String s2 = s1.substring(s1.indexOf("@")+1);
        System.out.println(s2);
    }
}

Output

Thus, in this way, we learn how to find a username from the email ID in Java. Also, learn how to find domain names from the string.