Java - 在尝试/捕获期间弹出的随机"null"

  • 本文关键字:随机 null Java java
  • 更新时间 :
  • 英文 :


我有一个脚本,用于测试为学习Try/Catch而输入的字符的有效性。数字必须以严格的"$123.45"格式输入,否则Try/Catch将打印错误。它需要能够捕获所有整数错误并打印它们。目前,如果一美元和一美分输入错误,脚本会正常工作,并打印两个错误。但是,如果在输入美分时只发现一个错误,则打印行显示以"null"开头。

示例(工作(:输入:请在表格$#.##中输入销售金额("q"退出(:$12d.de

打印:无效的美元格式-对于输入字符串:"12d">

无效的美分格式-对于输入字符串:"de">

示例(无效(:输入:请在表格$#.##中输入销售金额("q"退出(:$123.de

打印:null无效的美分格式-对于输入字符串:"de">

错误出现在myCent:的Catch中的"+="中

"myBad+="无效的美分格式-对于输入字符串:\"+mySale.substring(mySale.indexOf('.'(+1,mySale.length(((+"\"\n";">

如何使Catch在不丢失多错误打印功能的情况下不打印"null"?非常感谢所有帮助我们的人。

这个程序由以下两个脚本组成,"DKSaleCheck.java"是我的问题子:DKUnit6Ch15.java

import java.util.*; //Load all Utility Classes
public class DKUnit6Ch15 { //Begin Class DKUnit6Ch15
public static void main(String[] args) { //Begin Main
Scanner myScan = new Scanner(System.in); //Initialize the Scanner
String myAmount; //Define a new Variable
while(true) { //Begin infinite While Loop
System.out.print("Please enter amount of sale in form $#.## ("q" to quit): "); //Print the text
myAmount = myScan.next(); //Define a new Variable with the next user input
if(myAmount.equalsIgnoreCase("q")) { //Begin If Statement (if the user entered "q" then do the following...)
break; //Break the script
} //End If Statement
DKSaleCheck myCash = new DKSaleCheck(myAmount); //Define a new Variable and send it to the Constructor
myCash.print(); //Print the Output from the Constructor
} //End infinite While Loop
myScan.close(); //Close the Scanner
} //End Main
} //End Class DKUnit6Ch15

DKSaleCheck.java

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 + " centsn"); //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
}

像冠军一样工作!

最新更新