1

I know how to do this in PHP so I'm hoping I can do it in Java as well. Here is some of my Java code:

String curYear = form.getCurYear();
if(curYear == null || curYear.length() == 0 || !curYear.matches(decimalRegex)) {
  errors.rejectValue("curYear", "supplier.curYear.invalid", "Invalid format for dollar amount");
}

String lastYear = form.getLastYear();
if(lastYear == null || lastYear.length() == 0 || !lastYear.matches(decimalRegex)) {
  errors.rejectValue("lastYear", "supplier.lastYear.invalid", "Invalid format for dollar amount");
}

These 2 chunks are followed by several more almost identical chunks where just the variable names/strings change. So I wanted to create a for(or foreach) loop to create all these using an array of the variable names. But for the form.getCurYear(); code, the first character of the variable name(curYear) is capitalized, so I would need to construct the variable name beforehand. Easy enough: String capField = inputField.substring(0, 1).toUpperCase() + inputField.substring(1);

Now how do I use that variable name in the form property call? I know form.capField() won't work. Is this even possible?

3 Answers 3

4

You could Java 8 Supplier to do something like this:

validate(form::getCurYear, "curYear", decimalRegex, errors);
validate(form::getLastYear, "lastYear", decimalRegex, errors);

And your validate method could look like this:

private static void validate(Supplier<String> method, String name, String decimalRegEx, Errors errors) {
    String val = method.get();
    if (val==null || val.length()==0 || !val.matches(decimalRegEx)) {
        errors.rejectValue(name, "supplier." + name + ".invalid", "Invalid format for dollar amount");
    }
}

You could declare decimalRegex and errors as globals or class members to shorten the signature of validate.

Sign up to request clarification or add additional context in comments.

Comments

3

You can do this

    Map<String,String> map = new HashMap<String,String>();

    map.put("curYear", form.getCurYear());      
    map.put("lastYear", form.getLastYear());

    for (Map.Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        if(value == null || value.length() == 0 || !value.matches(decimalRegex)) {
              errors.rejectValue(key, "supplier"+key+".invalid", "Invalid format for dollar amount");
        }          
    }

This creates the map of name and value and use the value for checking and name for printing.

3 Comments

Why HashMap is needed here? Better to place strings into two arrays and loop through ones
Readability. With arrays if we have 20 variables, I have count manually where I have to place the key and value.
Didn't get the point. HashMap needed when value retrieved by key.
1

In Java, variable names are only available at compile time. One way to do something like what you want here is to use a Map with the name as a key. You might also be able to do create a solution using a class.

5 Comments

@dmikester1 After looking more closely at your code, you should also consider putting the duplicated code into a method which you can call with different values.
So my method would be something like this? private static checkField(String input, String capInput)
@dmikester1 Yes, something like that will probably work.
Then I run into the same issue of how do I call form.capInput()?
@dmikester1 I don't understand. What is form? How is it declared? And what does capInput() do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.