2012-12-16 29 views
0

我在使用JAVA進行電子郵件驗證時遇到問題(新增功能)不斷收到這些錯誤,並對原因感到困惑。我應該得到這些人的電子郵件,檢查它的設置標準(由我的教授),然後獲得有關測試的信息。當我檢查電子郵件時,我應該顯示錯誤,如果有的話。Java電子郵件驗證中的編譯器錯誤

錯誤

N:\Programming\Java\Homework\EmailAndGrade.java:29: illegal start of expression 
    private static boolean validEmail(sEmail); 
    ^
N:\Programming\Java\Homework\EmailAndGrade.java:29: illegal start of expression 
    private static boolean validEmail(sEmail); 
      ^
N:\Programming\Java\Homework\EmailAndGrade.java:29: ';' expected 
    private static boolean validEmail(sEmail); 
       ^
3 errors 

代碼

public class EmailAndGrade 
{ 

    public static void main(String[] args) 
    { 
     //Variables 
     String sEmail, sError, sTest; 
     int iTest; 
     char cGrade; 

     sEmail = JOptionPane.showInputDialog("Enter your email: "); 


     private static boolean validEmail(sEmail); 
       { 
       // editing to make requirements listed 
       // return email.matches("[A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{2,4}"); 
       return email.matches("[A-Z0-9]{7}[email protected][springfieldcollege]+\\.[edu]"); 
       }; 

     sError = "One or More Errors with the Input Email.\n" + 
       "Email is Empty.\n" + 
       "Email must be less than 30 characters.\n" + 
       "Must end in: @springfieldcollege.edu.\n" + 
       "Can only have one @.\n" + 
       "Must start with A through Z or a through z.\n" + 
       "Can not have: # $ % & ,\n "; 

     sTest = JOptionPane.showInptDialog("Enter Test Grade: "); 

     iTest = Integer.parseInt(sTest); 

     System.exit(0); 


    } 

    /* 
     void checkEmails() 
     { 
      for(String email : emailAddresses) { 
      if(validEmail(email)) { 
       // it's a good email - do something good with it 

      } 
      else { 
       // it's a bad email - do something... bad to it? sounds dirty... 
       JOptionPane.showMessageDialog(null, 
               sError, 
               "Email Error - Killian O'Brien", 
               JOptionPane.INFORMATION_MESSAGE); 
       } 
    }*/ 



} 

*編輯

好了,現在我有這樣的代碼

package com.mkyong.regex; 

import javax.swing.*; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class EmailAndGrade 
{ 

public static void main(String[] args) 
{ 
    //Variables 
    String sEmail, sError, sTest; 
    int iTest; 
    char cGrade; 

    sEmail = JOptionPane.showInputDialog("Enter your email: "); 


    sError = "One or More Errors with the Input Email.\n" + 
      "Email is Empty.\n" + 
      "Email must be less than 30 characters.\n" + 
      "Must end in: @springfieldcollege.edu.\n" + 
      "Can only have one @.\n" + 
      "Must start with A through Z or a through z.\n" + 
      "Can not have: # $ % & ,\n "; 

    sTest = JOptionPane.showInptDialog("Enter Test Grade: "); 

    iTest = Integer.parseInt(sTest); 

    System.exit(0); 


} 

public static boolean validEmail(String sEmail){ 
     return email.matches("[A-Z0-9]{7}[email protected][springfieldcollege]+\\.[edu]"); 
    } 

public static void checkEmails(){ 
    for(String sEemail : emailAddresses) { 
     if(validEmail(email)) { 
      // it's a good email - do something good with it 
     } 
     else { 
      // it's a bad email - do something... bad to it? sounds dirty... 
      JOptionPane.showMessageDialog(null, 
              sError, 
              "Email Error - Killian O'Brien", 
              JOptionPane.INFORMATION_MESSAGE); 
       } 
      } 
      } 
} 

現在我得到這些

N:\Programming\Java\Homework\EmailAndGrade.java:37: cannot find symbol 
symbol : method showInptDialog(java.lang.String) 
location: class javax.swing.JOptionPane 
     sTest = JOptionPane.showInptDialog("Enter Test Grade: "); 
         ^
N:\Programming\Java\Homework\EmailAndGrade.java:47: cannot find symbol 
symbol : variable emailAddresses 
location: class com.mkyong.regex.EmailAndGrade 
     for(String email : emailAddresses) { 
         ^
N:\Programming\Java\Homework\EmailAndGrade.java:54: cannot find symbol 
symbol : variable sError 
location: class com.mkyong.regex.EmailAndGrade 
               sError, 
              ^
N:\Programming\Java\Homework\EmailAndGrade.java:63: cannot find symbol 
symbol : variable email 
location: class com.mkyong.regex.EmailAndGrade 
     return email.matches("[A-Z0-9]{7}[email protected][springfieldcollege]+\\.[edu]"); 
      ^
4 errors 

當我打字時,我意識到我只是有一堆未聲明的變量。這是正確的嗎?

+0

你有一個分號後的分號'}; ' –

+1

您不能在Java中的方法中使用方法定義。另外,在你的方法聲明之後還有一個分號。檢查一下,看看你能不能解決它。 –

回答

2

您正在實施方法validEmail另一種方法main這是不允許的。將你的方法移出主函數(就在你最後一行之前(在課程結束大括號}之前))。 另外你在方法實現中的兩個地方使用額外的分號(第一行結束和最後一行結束)。刪除分號;。最後在方法簽名中添加參數類型類(String)。更新後的方法應如下所示:

private static boolean validEmail(String sEmail){ 
     // editing to make requirements listed 
     // return email.matches("[A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{2,4}"); 
     return email.matches("[A-Z0-9]{7}[email protected][springfieldcollege]+\\.[edu]"); 
    } 
1

您不能內聯聲明函數。將validEmail()的聲明移至main()之外。也可以在方法聲明結束時刪除分號。