我在一個名爲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
類調用它,我不明白如何使用該方法處理異常。
調用_pc.validate()_創建_PersonalContact_後,或者使方法變爲靜態。 – Berger
注意:這些應該是'IllegalArgumentException's,而不是'IllegalStateException's。這些異常是由傳遞給方法的參數引起的,而不是對象的狀態。 –
目前還不清楚你想要做什麼以及你期望發生什麼。這段代碼是否編譯?你的(無參數)'validate()'方法在哪裏?你爲什麼不打電話給你的其他'validate'方法?你不應該在收到輸入後進行驗證,而不是之前? – shmosel