Vas*_* De 1 java string string-length
我有一个字符串
String A;
Run Code Online (Sandbox Code Playgroud)
和一些处理它的方法
public void setA(String A) throws AInvalidException{
if(isAValid())
this.A = A;
throw new AInvalidException();
}
public boolean isAValid(){
int aLength = getA().length();
return b = (aLength==9) ? true: false;
}
public String getA(){
return A;
}
Run Code Online (Sandbox Code Playgroud)
当我试图在主要部分看到我的弦长
AClass.setA("123456789");
Run Code Online (Sandbox Code Playgroud)
它说我的String无效,调试后我看到我的String长度为零.我可能做错了什么?
验证时,您正在从您设置它的位置进行读取
改为
if(isAValid(A))
Run Code Online (Sandbox Code Playgroud)
和
public boolean isAValid(String str){
if(str == null) return false; //it could be null as well
int aLength = str.length();
return b = (aLength==9) ? true: false;
}
Run Code Online (Sandbox Code Playgroud)
注意:您应该遵循命名约定