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

时间:2018-11-21 23:38:12

标签: java

我有一个脚本,可以测试输入的字符的有效性以学习“尝试/捕捉”。必须以严格的“ $ 123.45”格式输入数字,否则“尝试/捕获”将显示错误。它需要能够捕获所有整数错误并将其打印出来。当前,如果错误地输入了美元和美分,则脚本可以正常工作并打印两个错误。但是,如果仅在输入美分时发现错误,则打印行会以“ null”开头。

示例(有效): 输入: 请以$#。##形式输入销售金额(“ q”退出):$ 12d.de

打印: 美元格式无效-输入字符串:“ 12d”

无效的美分格式-输入字符串:“ de”

示例(无效): 输入: 请以$#。##形式输入销售金额(“ q”退出):$ 123.de

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

错误在于myCent的Catch中的“ + =“:

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

如何在不丢失多错误打印功能的情况下使Catch不打印“空”?任何和所有对我们的帮助,我们深表感谢。

该程序由以下两个脚本组成,“ 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 + " cents\n"); //Print the text
        } //End Else Statement

    } //End Print Method

} //End Class DKSaleCheck

1 个答案:

答案 0 :(得分:0)

Scary Wombat的回答是最好的。我更改了:

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
}

像冠军一样工作!