title: Java - random "null" popping up during a Try/Catch
body: <p>I have a script that tests the validity of characters entered in order to learn Try/Catch. The numbers must be entered in a strict "$123.45" format or else the Try/Catch prints an error. It needs to be able to catch all integer errors and print them. Currently, if a dollar AND a cent are entered incorrectly the script works fine and prints both errors. However, if ONLY an error is found when entering cents, the print line says begins with "null". </p>
  
  <p><strong>Example (working):
  Input:</strong>
  Please enter amount of sale in form $#.## ("q" to quit): $12d.de</p>
  
  <p><strong>Print:</strong>
  Invalid dollar format - For input string: "12d"</p>
  
  <p>Invalid cents format - For input string: "de"</p>
  
  <p><strong>Example (NOT working):
  Input:</strong>
  Please enter amount of sale in form $#.## ("q" to quit): $123.de</p>
  
  <p><strong>Print:</strong>
  nullInvalid cents format - For input string: "de"</p>
  
  <p>The error is in the "+=" in the Catch for myCent: </p>
  
  <p>"myBad += "Invalid cents format - For input string: \"" + mySale.substring(mySale.indexOf('.') + 1,mySale.length()) + "\"\n";"</p>
  
  <p>How do I get the Catch to not print "null" without losing the multi-error print functionality? Any and all help us GREATLY appreciated. </p>
  
  <p>This program consists of the below two scripts, the "DKSaleCheck.java" is my problem child:
  <strong>DKUnit6Ch15.java</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>DKSaleCheck.java</strong></p>
  
  <pre><code>    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
  </code></pre>