【发布时间】:2014-12-09 15:06:17
【问题描述】:
代码还远未完成,但在我可以继续前进之前,我基本上被困在这上面。继续获取局部变量可能尚未在 theirPhone 上初始化。如果我移动构造函数通过 try catch,我会在 try catch 上收到错误,如果我保持原样,我会在 theirPhone.getAreaCode(phoneNumber );有什么帮助吗?
import java.util.Scanner;
public class CustomerTelephone {
public static void main(String[] args) {
CustomerTelephone theirPhone;
String phoneNumber = "407 407 4074";
try {
theirPhone = new CustomerTelephone(phoneNumber);
} catch (InvalidTelephoneException ite) {
System.out.println("Invalid telephone number format.");
}
theirPhone.getAreaCode(phoneNumber);
}
public CustomerTelephone(String telephone) throws InvalidTelephoneException {
if (telephone.length() != 12) {
throw new InvalidTelephoneException(
"The phone number was entered incorrectly.");
}
}
public String getAreaCode(String phoneNumber) {
String goBack;
String[] teleArray = phoneNumber.split("(?!^)");
goBack = teleArray[0 - 2];
return goBack;
}
public String getExchange(String phoneNumber) {
String goBack = null;
return goBack;
}
public String getLocalNumber(String phoneNumber) {
String goBack = null;
return goBack;
}
}
【问题讨论】:
标签: java variables methods constructor local