2016-02-15 35 views
-3

我在一個名爲PersonalContact的類中有此方法。調用一個方法來檢查錯誤的輸入

public void validate(String name, int age, String address, String city, String state, String zip){ 

    //If the name field is the empty string or null, then throw a NullPointerException. 
    if (name.equals(null)){ 
     throw new NullPointerException(); 
    } 
    //If the age field is not between 1-100, then throw an IllegalStateException 
    if (age < 1 || age > 100){ 
     throw new IllegalStateException(); 
    } 
    //If the address or city field is the empty string or null, then throw a NullPointerException. 
     if(address.equals(null) || city.equals(null)){ 
      throw new NullPointerException(); 
     } 
    //If the state field is not exactly 2 characters, then throw an IllegalStateException 
     if (state.length() != 2){ 
      throw new IllegalStateException(); 
     } 
    //If the zip code field is not exactly 5 numeric characters, then throw an IllegalStateException 
     if (zip.length() != 5){ 
      throw new IllegalStateException(); 
     } 

    } 

@Override 
public void validate(String name, int age) { 


} 

我試圖調用驅動程序的方法:

import java.util.Scanner; 

public class PlannerMain { 
static Scanner scanner = new Scanner(System.in); 

public static void main(String[] args) { 

    while (true) { 

    System.out.println("Create new contact?"); 
    System.out.println("1.Personal contact "); 
    System.out.println("2.Business Contact "); 
    System.out.println("3.Exit."); 

    int option = scanner.nextInt(); 
    boolean pcLoop = true; 

    if (option == 1) { // Create Personal Contact 

    do { 
     try { 
     validate(); 

     System.out.println("Name?(No spaces)"); 
     String name = scanner.next(); 

     System.out.println("Age?"); 
     int age = scanner.nextInt(); 

     System.out.println("Address?(No Spaces)"); 
     String address = scanner.next(); 

     System.out.println("City?"); 
     String city = scanner.next(); 

     System.out.println("State?"); 
     String state = scanner.next(); 

     System.out.println("Zip"); 
     String zip = scanner.next(); 

     PersonalContact pc = new PersonalContact(name, age, address, city, state, zip); 
     System.out.println(pc.toString()); // Prints out the 
     // contact info 
     pcLoop = false; // Ends the loop and goes back to the 
     // menu 
     } catch (Exception age) { 
     System.out.println("Please enter name without spaces."); 
     } 

    } while (pcLoop); // Ends option 1 

    } // End option 1 
    else if (option == 2) { // Create Business Contact 

    System.out.println("Name?(No spaces)"); 
    String name = scanner.next(); 

    System.out.println("Age?"); 
    int age = scanner.nextInt(); 

    System.out.println("Business Phone?"); 
    String businessPhone = scanner.next(); 

    System.out.println("Cellphone?"); 
    String cellPhone = scanner.next(); 

    BusinessContact bc = new BusinessContact(name, age, businessPhone, cellPhone); 

    System.out.println(bc.toString()); 

    } // End option 2 
    else if (option == 3) { /** Terminates the program */ 
    System.exit(0); 
    } // End option 3 
    } // End while 

    } // End void main 
} // End 

我想用try-catch塊工作趕不恰當的用戶輸入在我PersonalContact各個領域的方法。我想爲我的BusinessContact做同樣的事情。我知道我應該在try塊中調用方法,但我不明白爲什麼它不會從PersonalContact類調​​用它,我不明白如何使用該方法處理異常。

+0

調用_pc.validate()_創建_PersonalContact_後,或者使方法變爲靜態。 – Berger

+0

注意:這些應該是'IllegalArgumentException's,而不是'IllegalStateException's。這些異常是由傳遞給方法的參數引起的,而不是對象的狀態。 –

+0

目前還不清楚你想要做什麼以及你期望發生什麼。這段代碼是否編譯?你的(無參數)'validate()'方法在哪裏?你爲什麼不打電話給你的其他'validate'方法?你不應該在收到輸入後進行驗證,而不是之前? – shmosel

回答

0

你不能調用實例方法,直到你有一個實例。你需要打電話。

PersonalContact pc = new PersonalContact(name, age, address, city, state, zip); 
pc.validate(); 

然後,您不需要通過驗證任何參數,因爲它們是PC的狀態的一部分。驗證應該看起來更像這樣。

public void validate() { 
    //If the name field is the empty string or null, then throw a NullPointerException. 
    if (this.name == null || this.name.equals("")){ 
     throw new NullPointerException(); 
    } 
    //If the age field is not between 1-100, then throw an IllegalStateException 
    if (this.age < 1 || this.age > 100){ 
     throw new IllegalStateException(); 
    } 
    //If the address or city field is the empty string or null, then throw a NullPointerException. 
     if(this.address == null || this.city == null){ 
      throw new NullPointerException(); 
     } 
    //If the state field is not exactly 2 characters, then throw an IllegalStateException 
     if (this.state.length() != 2){ 
      throw new IllegalStateException(); 
     } 
    //If the zip code field is not exactly 5 numeric characters, then throw an IllegalStateException 
     if (this.zip.length() != 5){ 
      throw new IllegalStateException(); 
     } 
} 

請注意,myString.equals(null)不起作用。你必須使用myString == null。儘管如果你從鍵盤輸入數據,你很可能會有一個空字符串比空字符串,所以使用myString.equals(「」)。

如果你真的想調用validate方法沒有實例PersonalContact,你需要讓你的第一個validate方法靜態和稱呼它

PersonalContact.validate(name, age, address, city, state, zip); 

我建議也對普通的面向對象編程讀了一下因爲你似乎沒有完全掌握它。

1

正如其他人所說,你沒有實例化你的對象,所以你不能調用驗證一些尚不存在的東西。 我也想提出一個建議。你爲什麼不嘗試把所有這些驗證都放在每個setter方法中。然後,您可以從您的PersonalContact的構造函數中調用這些。例如,你的PersonalContact類中:

private String name; 
private int age; 
... 

//This should throw all the types of exceptions 
//It should look like public PersonalContact(...){ throws NullPointerException, IllegalStateException , etc. 
public PersonalContact(name, age, address, city, state, zip){ 
    setName(name); 
    setAge(age); 
    ... 
} 

public void setName(String name) throws NullPointerException { 
    //If the name field is the empty string or null, then throw a NullPointerException. 
    if (name == null || name.isEmpty()){ 
     throw new NullPointerException(); 
    } 
    this.name = name; 
} 

public void setAge(int Age) throws IllegalStateException { 
    //If the age field is not between 1-100, then throw an IllegalStateException 
    if (age < 1 || age > 100){ 
     throw new IllegalArgumentException(); //as someone in the comments mentioned, use this instead of IllegalStateException 
    } 
    this.age = age; 
} 
... 

你可以繼續做這些你所有的其他方法,並通過構造函數調用它,當你創建對象時,它會驗證領域。