vendredi 27 novembre 2015

Trying to stop an ActionPerformed from executing certain code if an exception is thrown

I have been working on this code for a few days now and cant figure out how to fix one specific thing about it. I want to be able to stop the code from executing below the

this.errors();

if (error == false) {

//this.errors();

//Part of the code. 


private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    errorTextArea.setText(null);
    try {
        String stringLength = lengthTextField.getText();
        lengthInMeters = Double.parseDouble(stringLength);

        String stringDiameter = diameterTextField.getText();
        diameterInInches = Double.parseDouble(stringDiameter);

        String quantity = quantityTextField.getText();
        finalQuantity = Integer.parseInt(quantity);

        this.errors();
        if (error == false) {
            //this.errors();
       } else {

            /**
             * Checks which check boxes and radio buttons are selected and
             * returns true.
             *
             */
            if (chemicalResistanceCheckBox.isSelected()) {
                chemical = true;
            }

            if (outerReinforcementCheckBox.isSelected()) {
                outerReinforcement = true;
            }

            if (innerInsulationCheckBox.isSelected()) {
                innerInsulation = true;
            }
            if (zeroColourRadioButton.isSelected()) {
                colourInput = 0;
            }
            if (oneColourRadioButton.isSelected()) {
                colourInput = 1;
            }
            if (twoColourRadioButton.isSelected()) {
                colourInput = 2;
            }
            // Creates pipe, gets the size then returns a quote.
            this.createPipe();
            double sizeOfPipe = this.orderedSize(lengthInMeters, diameterInInches);

            // Prints to the text areas.
            double costOfSelectedPipe = quote * sizeOfPipe;
            cartTextArea.setText(cartTextArea.getText() +"\n"+ "Pipe price:  " + df.format(costOfSelectedPipe) + "\n");
            double costOfSelectedOrder = costOfSelectedPipe * finalQuantity;
            prices.add(costOfSelectedOrder);
            currentOrderTotal.add(costOfSelectedOrder);
            cartTextArea.append("Total: " + df.format(costOfSelectedOrder) + "\n");
            grandTotal = this.finalTotal();
            orderTotal = this.currentTotal();

            currentOrderPriceLabel.setText("£"+df.format(orderTotal));
            totalPriceLabel.setText("£"+df.format(grandTotal));

        }
    } catch (NumberFormatException e) {
        errorTextArea.append("Please input numeric characters");
    }

} 

Below this we have a method for error messages we would like to be called if the try catch throws an exception. Currently if you set the value of error to be true it will work perfectly but not print out the specific error messages we want, and if you set it to false when you try and use the submit button to on the gui we have created nothing will happen at all it seems to get stuck.

private void errors(){

    if (lengthInMeters > 6.0 || lengthInMeters < 0.10) {
            errorTextArea.append("\n" +"Input a length between 0.1m to 6m, please.");
            error = true;
    }
    if (diameterInInches < 1 || diameterInInches > 30) {
            errorTextArea.append("\n" +"Please input a diameter between 1 and 30 inches");
            error = true;
        }
    if (finalQuantity > 300 || finalQuantity < 1) {
            errorTextArea.append("\n" +"Please order at least one pipe, but less than 300");
            error = true;
        }

}      

I have a link to github with all the code in it, if that would make it easier id very much appreciate some help here if anyone has anything. even if I am just plain wrong in what it is I am trying to do.

http://ift.tt/1TblOT3

example of the program working

enter image description here

Aucun commentaire:

Enregistrer un commentaire