Question

I want to pass System.out.println(); as an argument but the compiler won't allow me to return a void type as an argument. here is what I want it for.

public class Array {

    public static void main(String args[]) {
        a(data());
    }

    static void a(e) {
        System.out.println(e);
    }

    static void data() {
        ...
    }
}

So What is want a(data()); to look like after it is compiled is something like this.

a(data()) = System.out.println(data(){...});

Eventually I want to shorthand System.out.println().

Was it helpful?

Solution

What you are doing here is not passing System.out.println() as an argument; you are trying to pass an argument to System.out.println()

Try changing the return type of data() to String, or int, or anything other than void, and return something of that type from it.

Also change the parameter type of e in the function definition of a() to match the return type of data().

After you make these changes, calling a(data()); will actually print something out.

Example:

public static void main(String args[]) {
    a(data());
}

// shorthand for System.out.println
static void a(String e) {
    System.out.println(e);
}

// a method that returns some data
static String data() {
    // replace this with whatever actual data you want to return
    return "This is some data...";
}

OTHER TIPS

If you just want to shorthand System.out.println, then have a method with return type void that accepts a string argument and inside of the method just do:

System.out.println(argument)

As mentioned by others, you don't want/need to pass System.out.println as a method argument. However, if you would like to do that (one never knows...), you could do that in Java 8 with Lambda expressions.

Create a functional interface:

@FunctionalInterface 
public interface Action {
    void run(String param);
}

Passing this interface to a method:

public class MyClass {
    public void execute(Action action){
       action.run("Hello!");
    }
}

Use this class:

MyClass c = new MyClass();
c.execute(System.out::println);

Simply save your println statements to String and return it for printing
Your code:

static void data() {
    int array[] = {1,5,6};
    int alength = array.length;
    System.out.println("  Location\tData");
    for(int i=0;i<alength;i++) {
        System.out.println("  " + i + "\t\t" + array[i]);
    }
}

Change to:

static String data() {
    int array[] = {1,5,6};
    int alength = array.length;
    //Note extra \n symbol for new line
    String result = "  Location\tData\n";
    for(int i=0;i<alength;i++) {
        result += "  " + i + "\t\t" + array[i] + "\n";
    }
    return result;
}

then modify your a() method to accept String as a parameter:

static void a(String result) {System.out.println(result);}

A working example could look like this:

public class Array {
    public static void main(String args[]) {
        println(data());
    }

    // I strongly advise to use understandable naming
    // a() is completely uninformative
    static void println(String result) {
        System.out.println(result);
    }

    static String data() {
        int array[] = { 1, 5, 6 };
        int alength = array.length;
        // Note extra \n symbol for new line
        String result = "  Location\tData\n";
        for (int i = 0; i < alength; i++) {
            result += "  " + i + "\t\t" + array[i] + "\n";
        }
        return result;
    }
}

While the compiler may not allow you to pass a function pointer, it should allow you to pass blocks.

In Objective-C; a block as the following syntax:

void (^action)(NSString *s) = ^(NSString *s){ NSLog(s); }

You can then pass your "action" block around as a parameter, and call it whenever required:

action(@"Hello World");

Blocks are available in all recent variations of C, Wikipedia has a nice article on the subject at http://en.wikipedia.org/wiki/Blocks_(C_language_extension).

I want to change this code

    package Array;
import java.util.Random;

public class Array {
    public static void main(String args[]) {
        data();
        a();
        random();
        a();
        array();
        a();
        rows();
        a();
        System.out.println("average " + average(45,15,15,48,97,45));

    }

    static String s() {
        return "helloWorld";
    }

    static void a() {System.out.println();}

    static void data() {
        int array[] = {1,5,6};
        int alength = array.length;
        System.out.println("  Location\tData");
        for(int i=0;i<alength;i++) {
            System.out.println("  " + i + "\t\t" + array[i]);
        }
    }

    static void random() {
        Random rdm = new Random();
        int freq[] = new int[7];

        for(int i=1;i<1000;i++) {
            ++freq[1+rdm.nextInt(6)];
        }
        System.out.println("Face\tFrequency");
        int frequence = freq.length;
        for(int face=1;face<frequence;face++) {
            System.out.println(face+"\t"+freq[face]);
        }
    }

    static void array() {
        String po[] = {"lala","po","tinkiwinki","disty"};
        for(String lala: po) {
            System.out.print(lala + " ");
        }
        System.out.println();
    }

    static void rows() {
        int arrays[][]= {{1,5,78,15},{45,67},{875,15687,158,4515,23,2,2}};

        for(int i=0;i<arrays.length;i++) {
            for(int j=0;j<arrays[i].length;j++) {
                System.out.print(arrays[i][j]+"\t");
            }
            System.out.println();
        }
    }

    static int average(int...numbers) {
        int total=0;
        for(int x:numbers)
            total += x;
        return total/numbers.length;
    }
}
class time {
    int h, m, s;
    void setTime(int hour,int minute,int second) {
        h = ((hour>=0 && hour<=24) ? hour : 0);
        m = ((minute>=0 && minute<=60) ? minute : 0);
        s = ((second>=0 && second<=60) ? second : 0);
    }


into this type of code for main.


    a(data());
    a(random());
    a(array());
    a(rows());
a("average " + average(45,15,15,48,97,45));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top