Java—在Try/Catch过程中出现随机“null”
我有一个脚本,测试输入的字符的有效性,以便学习Try/Catch。必须以严格的$123.45格式输入数字,否则Try/Catch会打印错误。它需要能够捕获所有整数错误并打印它们。目前,如果输入的一美元和一美分不正确,脚本将正常工作并打印这两个错误。但是,如果在输入美分时仅发现错误,则打印行显示以null开头 工作示例: 输入: 请在表格$中输入销售金额。q退出:12天 打印: 无效的美元格式-用于输入字符串:12d 无效的美分格式-用于输入字符串:de 示例不起作用: 输入: 请在表格$中输入销售金额。q退出:$123.de 打印: Null无效的美分格式-用于输入字符串:de 错误在myCent的Catch中的+=中: myBad+=无效的美分格式-用于输入字符串:\+mySale.substringmySale.indexOf'.+1,mySale.length+\\n 如何让Catch在不丢失多错误打印功能的情况下不打印null?非常感谢所有帮助我们的人 该程序由以下两个脚本组成,DKSaleCheck.java是我的问题孩子: DKUnit6Ch15.java DKSaleCheck.javaJava—在Try/Catch过程中出现随机“null”,java,Java,我有一个脚本,测试输入的字符的有效性,以便学习Try/Catch。必须以严格的$123.45格式输入数字,否则Try/Catch会打印错误。它需要能够捕获所有整数错误并打印它们。目前,如果输入的一美元和一美分不正确,脚本将正常工作并打印这两个错误。但是,如果在输入美分时仅发现错误,则打印行显示以null开头 工作示例: 输入: 请在表格$中输入销售金额。q退出:12天 打印: 无效的美元格式-用于输入字符串:12d 无效的美分格式-用于输入字符串:de 示例不起作用: 输入: 请在表格$中输入销
恐怖袋熊有最好的答案。我改变了:
public DKSaleCheck(String mySale) {
myBad = null;
//rest of class
}
public void print(){
if(myBad != null)
//rest of class
}
致:
像冠军一样工作 您有myBad+=需要myBad=的地方。因为它最初是空的,所以会得到一个空值。但是如果我删除+它将不再找到倍数。它在只打印带有美分的错误后结束。例如,如果我输入$12d.de,它只显示美分部分的错误,而不是美元部分的错误。请格式化代码。在调试时,您可以看到在您的情况下什么是null;到myBad=;倍数是多少?一次只能得到一个异常。
class DKSaleCheck{ //Begin Class DKSaleCheck
int myDollar; //Define a new Variable
int myCent; //Define a new Variable
String myBad; //Define a new String Variable
public DKSaleCheck(String mySale) { //Begin Method DKSaleCheck and receive sale as a string
myBad = null; //Define a new Variable
if(!mySale.startsWith("$")) { //Begin If Statement (if mySale does NOT start with a "$")
myBad = "Invalid sale format missing \"$\" - " + mySale + "\n"; //Fill the Variable with the String data
} //End If Statement
else if(mySale.indexOf('.') == -1) { //Begin ElseIf Statement (if mySale does NOT contain a ".")
myBad = "Invalid sale format missing \".\" - " + mySale + "\n"; //Fill the Variable with the String data
} //End ElseIf Statement
else{ //Begin Else Statement
try{ //Begin Try Statement
myDollar = Integer.parseInt(mySale.substring(1, mySale.indexOf('.'))); //Fill the Variable with the data if ONLY integers are detected from Index 1 to the "."
} //End Try Statement
catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad = "Invalid dollar format - For input string: \"" + mySale.substring(1,mySale.indexOf('.')) + "\"\n"; //Fill the Variable with the String data
} //End Catch Statement
try{ //Begin Try Statement
myCent = Integer.parseInt(mySale.substring(mySale.indexOf('.') + 1,mySale.length())); //Fill the Variable with the data if ONLY integers are detected after the "."
} //End Try Statement
catch(Exception myError) { //Begin Catch Statement (if the subString does not contain ONLY integers)
myBad += "Invalid cents format - For input string: \"" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + "\"\n"; //Fill the Variable with the String data
} //End Catch Statement
} //End Else Statement
} //End Method DKSaleCheck
public void print(){ //Begin Print Method
if(myBad != null){ //Begin If Statement (if the error variable is NOT null)
System.out.println(myBad); //Print the String Variable
} //End If Statement
else{ //Begin Else Statement
System.out.println("$" + myDollar + "." + myCent); //Print the text
System.out.println(myDollar + " dollars and " + myCent + " cents\n"); //Print the text
} //End Else Statement
} //End Print Method
} //End Class DKSaleCheck
public DKSaleCheck(String mySale) {
myBad = null;
//rest of class
}
public void print(){
if(myBad != null)
//rest of class
}
public DKSaleCheck(String mySale) {
myBad = "";
//rest of class
}
public void print(){
if(myBad != "")
//rest of class
}