'Show the elements in a List in a dialog box in java

I created a bunch of try-catch blocks to catch several kinds of exception in java. Then I added those exception messages to a List.

What I want to do is that when a user clicks on a Save button, the application would check if any exception is thrown. If yes, then that exception is added to the List.

Then, I created a String, and use a for loop to get all the exception messages, so that I can display it in an Alert box.

The data is passed in through a form on an UI. I tried to check if the data type is the right type and the range of values should make sense. For example, min should be smaller than max, and stock level should be between min and max values.

Also, I think I should make sure that the Alert window only pops up when the List.size() > 0, right? I tried it, but it didn't work as expected, either. For example, I added this line before the Alert.

if(errorMessages.size() >) {
   Alert alert = new Alert.... // the rest of the code is attached in the next code secion
}

The problem is that it doesn't show all the error messages thrown. Instead, it only shows one of them. Here is what I have so far. Thank you very much.

double price;
        int max = Integer.MAX_VALUE;
        int min = Integer.MIN_VALUE;
        int stock = Integer.parseInt(addPartStockTxt.getText());
        int machineId;
        List<String> errorMessages = new ArrayList<>();

        // check and see if the price is double
        try {
            price = Double.parseDouble(addPartPriceTxt.getText());
        }
        catch (NumberFormatException eNumber) {
            errorMessages.add(eNumber.getMessage());
        }

        // check and see if the max is an integer
        try {
            max = Integer.parseInt(addPartMaxTxt.getText());
        }
        catch (NumberFormatException eNumber) {
            errorMessages.add(eNumber.getMessage());
        }

        // check and see if the min is an integer
        try {
            min = Integer.parseInt(addPartMinTxt.getText());
        }
        catch (NumberFormatException eNumber) {
            errorMessages.add(eNumber.getMessage());
        }

        // check and see if the stock is an integer
        try {
            stock = Integer.parseInt(addPartStockTxt.getText());
        }
        catch (NumberFormatException eNumber) {
            errorMessages.add(eNumber.getMessage());
        }

        // check and see if the MachineID is an integer
        try {
            machineId = Integer.parseInt(addPartSwitchTxt.getText());
        }
        catch (NumberFormatException eNumber) {
            errorMessages.add(eNumber.getMessage());
        }

        // check and see if the values of min, max and stock are reasonable
        try {
            if (min > max) {
                throw new Exception("Minimum value should be smaller than the maximum value.");
            }
            if ((min > stock) || (stock > max)) {
                throw new Exception("The value of inventory is outside the acceptable range.");
            }
        }
        catch (Exception ex) {
            errorMessages.add(ex.getMessage());
        }

        String errorMessageList = "";
        for (int i = 0; i < errorMessages.size(); i++) {
            errorMessageList += errorMessages.get(i) + "\n";
        }

        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Data Entry Error");
        alert.setHeaderText("Please verify and enter the correct values");
        alert.setContentText(errorMessageList);

        alert.showAndWait();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source