title: Mail address validation JAVA
body: <p>I'm trying to write a little program where I ask the user to enter an email address. And then I verify if its a valid email address or not. Like "example@example.com" would be valid, but "example@@example.com.org" would be invalid.</p>
  
  <p>Here's my code, I have made it work to detect if there is an @ and . character in there, but I need to make sure it only appears once, and that the @ appears before the dot (.)</p>
  
  <pre><code> import java.util.Scanner;
  
  public class email {
      public static void main(String args[])
      {
          Scanner skanni = new Scanner(System.in);
  
          System.out.println("Sládu inn email adressuna thína.");
          System.out.println("Email: ");
          String mail = skanni.nextLine();
  
          boolean b1=mail.contains("@") &amp;&amp; mail.contains(".");
  
          if (b1)
          {
              System.out.println("This email is valid");
          }else{
              System.out.println("this email is invalid");
          }
  
  
      }
  }
  </code></pre>
  
  <p>If anyone could help, it would be greatly appreciated!</p>
  
  <p>EDIT: Thanks everyone for your answers and help. I'm posting the final code below if anyone else will be doing something similar in the future. Oh and you can ignore the comment. You probably won't understand it :)</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.regex.*;
  
  public class email {
      public static void main(String args[])
      {
          Scanner skanni = new Scanner(System.in);
  
          System.out.println("Sládu inn email adressuna thína.");
          System.out.println("Email: ");
          String mail = skanni.nextLine();
  
          /*Skilgreining á eftirfarandi kóda
           * ^ táknid er byrjunin á strengnum
           * thad sem ég set innan sviga á eftir, er thad sem ég legg áherslu á í hverju sinni
           * [A-Za-z0-9] eru leyfilegir stafir í fyrsta lid emailsins, sbr: (birgir09)dæmi@gmail.com
           * + á milli táknar ad naesti svigi byrjar, thad er ad segja. (\\-[A-Za-z0-9-])
           * *@ táknar ad @ merkid er á milli sviganna
           * "[A-Za-z0-9] er thad sem kemur fyrir framan punktinn sbr. (gmail).com
           * (\\.[A-Za-z0-9]) er thad sem kemur fyrir aftan punkt sbr gmail.(com)
           */
          Pattern p = Pattern.compile("^[A-Za-z0-9-]+(\\-[A-Za-z0-9])*@"
                  + "[A-Za-z0-9-]+(\\.[A-Za-z0-9])");
          Matcher m = p.matcher(mail);
  
          if (m.find())
          {
              System.out.println("Thetta email er loglegt.");
          }else{
              System.out.println("Thetta email er ekki loglegt.");
          }
  
  
  
      }
  }
  </code></pre>