-3

Hi there I am new to java, studying in my bs course from a couple of weeks. So please don't mind if I ask something stupid. btw coming to the point. Assignment was that you have to write a program in which I have to break the email address into parts using StringBuffer e.g: if the given email is hassanaqve@gmail.com so I have to break it like username: hassanaqve domain name : gmail extension : .com

I tried myself but it is not working there are no errors but also not giving output

    import java.lang.*;
public class Lab5 {
    public static void main(String[] args) {
        StringBuffer email = new StringBuffer ("Hassanaqve@gmail.com");
         for ( int i=0; i<= email.length(); i++){
             if ( email.substring (i).equals ("@")){
                 System.out.println( "Username : " + email.substring(0,i));
                 int j=i;
                 for ( int p=0; j<= email.length(); p++ ){
                   if ( email.substring (p) == (".")){
                       System.out.println( "doamin : " + email.substring(j,p));
                       System.out.println( "extention : " + email.substring(p+1,p+4));
                   }  
                 }

             }

        }
    }
mpromonet
  • 11,326
  • 43
  • 62
  • 91
Muhammad Hassan
  • 424
  • 3
  • 5

1 Answers1

1

emailString.subString(0,emailString.indexOf('@')) will give you username

emailString.subString(emailString.lastIndexOf('.')) will give extention

emailString.subString(0,emailString.indexOf('@')+1,emailString.lastIndexOf('.')) will give you domsin name

Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32