SchoolJava101
SchoolJava101

Reputation: 769

Array Of Methods That Return A String

I'm creating a kind of data testing program, and one specific part is giving me a huge amount of trouble. In my main method class there is one section where I need to send over a String of data as a parameter in a method to my methods class (let's call it ValidatorClass) and the idea being that the method will then return any validation errors or if there are none simply an empty String.

This would be fine except that I use "for loops" when going through my data to validate as doing it without is just too clunky. I tried to research about arrays of methods and found plenty of useful things that work with void methods but found nothing on any methods that return variables.

In a nutshell I'm asking: Is it possible to create an array of methods (or implement an array of objects to simulate an array of methods) that return a variable?

Here is some example code, but in the actual program the method's return would actually be used further on:

public class Validation{
    public static void main(String args){
        ValidatorClass valTest = new ValidatorClass();
        String[] dataList = {"Andrew", "Jameson", "Male"}

        for(int i = 0; i < dataList.length; i++){
            String errors = valTest.testInput(dataList[i], i).validationList[i];
            System.out.println(errors);
        }
    }
}

And in ValidatorClass:

public class ValidatorClass{
    public String testInput(String data, int index){
        //Tests the data by calling method "index" which corresponds to data type.
        //ie. validateName would be index : 1, validateSurname index : 2 etc
        String errors = validationMethodList[index](data); //Somehow add data as a parameter to it
        return errors;
    }

    public String validateName(String name){
        String errors = "";
        if(name.length < 1){
            errors += "Name Not Entered";
        }
        return errors;
    }

    public String validateSurname(String surname){
        String errors = "";
        if(surname.length < 1){
            errors += "Surame Not Entered";
        }
        return errors;
    }

    public String validateGender(String gender){
        String errors = "";
        if(!gender.equalsIgnoreCase("male") || !gender.equalsIngoreCase("female")){
            errors += "Invalid Gender";
        }
        return errors;
    }
}

Upvotes: 1

Views: 132

Answers (1)

Jae Heon Lee
Jae Heon Lee

Reputation: 1101

I imagine that you have something like...

static String validate1(Validatible v) { /* do something */ }
static String validate2(Validatible v) { /* do something else */ }
static String validate3(Validatible v) { /* do something still else */ }

And that you want to execute, in some method...

Validatible v = getValidatible();
System.out.println(validate1(v));
System.out.println(validate2(v));
System.out.println(validate3(v));

Then perhaps you could write an interface:

public interface Validator {
  String validate(Validatible v);
}

...and keep them in an array or a list...

private static final List<Validator> validators = Arrays.asList(
  new Validator() {
    @Override
    public String validate() {
      /* do something */
    }
  },
  new Validator() {
    @Override
    public String validate() {
      /* do something else */
    }
  },
  new Validator() {
    @Override
    public String validate() {
      /* do something still else */
    }
  }
);
// Can be written more compactly if in Java 8.

Thereafter, you can call the methods in a for-loop:

Validatible v = getValidatible();
for(Validator validator : validators) {
  System.out.println(validator.validate(v));
}

Possible improvements would include using a StringBuilder to build a single String (or using the Stream API and using Collectors.joining) if this fits your purpose better.

Upvotes: 3

Related Questions