Search code examples
javamethodstypesreturn

Is it possible to return nothing with a non-void method if a certain condition is not met


Is there a way of returning nothing (not even null) with a non-void method if a certain condition is not met, but return something of the method type (like "int" if method is "int") if that condition is met?

Now that I have seen the comments, I had read about Optional class but couldn't figure it out. So if I have something like an ArrayList method, how'd I do it?

static ArrayList<String> method() {
    if (true) {
        return ArrayList; }
    else {
        ? } //(equivalent of return nothing, not even null) 

So it'd be something like this, but can you help with exact syntax?:

static Optional method() {
    if (true) {
        return ArrayList<String>; }
    else {
        Optional.empty() } //(equivalent of return nothing, not even null) 

Okay, there was a comment below, let me post my recursive function. What I want here is in the first if statement, there is a print and a return statement. But at the end of the function there is a necessary return null and thus the function returns null. I want function to return what it prints, i.e. indices, so I can use it as an input somewhere else.

static ArrayList<String> connectionFinder(String linInit, String linFin, ArrayList<String> indices, ArrayList<ArrayList<String>> linArray) {
        if (linInit.equals(linFin)) {    //returns the indices list if linInit reaches linFin
            System.out.println(indices);
            return indices;
        }
        for (int i = 0; i < linArray.size(); i++) {
            if (linArray.get(i).contains(linInit) && !indices.contains(i)) {     //checks which stations are exchange stations for linInit, and checks the index to prevent infinite recursion
                for (int j = 1; j < linArray.get(i).size(); j++) {
                    if (!linArray.get(i).get(j).equals(linInit)) {    //j starts from 1 so as to avoid the station name, and checks if the linInit is different to prevent infinite recursion
                        connectionFinder(linArray.get(i).get(j), linFin, add(indices, i), linArray);    //changes the linInit, and adds the index ( add(ArrayList,value) is a custom method that returns the ArrayList instead of a boolean -ArrayList.add(value) returns a boolean-)
                    }
                }
            }
        }
        indices.remove(indices.size()-1);
        return null;
    } 

Solution

  • I would write a separate function:

    public static ArrayList<String> connectionFinder(String linInit, String linFin, ArrayList<ArrayList<String>> linArray) {
      ArrayList<String> indices = new ArrayList<>();
      connectionFinder(linInit, linFin, indices, linArray);
      return indices;
    }
    
    

    And your function, now as a void:

    private static void connectionFinder(String linInit, String linFin, ArrayList<String> indices, ArrayList<ArrayList<String>> linArray) {
        if (linInit.equals(linFin)) {
            return;
        }
        for (int i = 0; i < linArray.size(); i++) {
            // Loop omitted for clarity
        }
        indices.remove(indices.size()-1);
    }