Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1029135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:29:52+00:00 2026-05-16T12:29:52+00:00

I want to write a method in Java that verifies that some conditions hold

  • 0

I want to write a method in Java that verifies that some conditions hold on some data, and acknowledges that the data is valid or produces an appropriate error message otherwise.

The problem is that we cannot return more than one thing from a method, so I’m wondering what the best solution is (in terms of readability and maintainability).

First solution. Easy, but we cannot know what exactly made the check fail:

boolean verifyLimits1(Set<Integer> values, int maxValue) {
    for (Integer value : values) {
        if (value > maxValue) {
            return false; // Out of limits
        }
    }
    return true; // All values are OK
}

Second solution. We have the message, but we are using exceptions in a way that we shouldn’t (besides, it should probably be a domain-specific checked exception, too much overhead IMO):

void verifyLimits2(Set<Integer> values, int maxValue) {
    for (Integer value : values) {
        if (value > maxValue) {
            throw new IllegalArgumentException("The value " + value + " exceeds the maximum value");
        }
    }
}

Third solution. We have a detailed message, but the contract is not clean: we make the client check whether the String is empty (for which he needs to read the javadoc).

String verifyLimits3(Set<Integer> values, int maxValue) {
    StringBuilder builder = new StringBuilder();
    for (Integer value : values) {
        if (value > maxValue) {
            builder.append("The value " + value + " exceeds the maximum value/n");
        }
    }
    return builder.toString();
}

Which solution would you recommend? Or is there a better one (hopefully!)?

(Note: I made up this little example, my real use case concerns complex conditions on heterogeneous data, so don’t focus on this concrete example and propose Collections.max(values) > maxValue ? "Out of range." : "All fine." :-).)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:29:52+00:00Added an answer on May 16, 2026 at 12:29 pm

    If you need more than a single value you should return a simple class instance instead. Here is an example of what we use in some cases:

    public class Validation {
        private String          text    = null;
        private ValidationType  type    = ValidationType.OK;
    
        public Validation(String text, ValidationType type) {
            super();
            this.text = text;
            this.type = type;
        }
        public String getText() {
            return text;
        }
        public ValidationType getType() {
            return type;
        }
    }
    

    This uses a simple Enumeration for the type:

    public enum ValidationType {
        OK, HINT, ERROR;
    }
    

    A validator method could look like this:

    public Validation validateSomething() {
        if (condition) {
            return new Validation("msg.key", ValidationType.ERROR);
        }
        return new Validation(null, ValidationType.OK);
    }
    

    That’s it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write an average method in java such that it can consume
I want to write a method that run multiple threads and I want before
I want to write a C# method that can accept any number. Something like:
I want to write a method that when supplied an array of ints will
I am attempting to write some Java code that takes an image from a
I want to write an application in Java that will communicate with Google App
I want to write a method that when supplied an array of ints will
I am creating a jasper report.In that I want to write one method which
I've been trying to write some very fast Java code that has to do
Imagine, I want to write a useless method called: isInstanceof that returns a boolean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.