-3

I have two methods (not using a main method in this example), and I'm trying to pass the String array "transactions" found in the TransactionName method to the ArrayPrinter method and print it out. No return statements if possible.

Yes, I'm aware that the method headings probably need fixing to allow it to happen, so I'd like some help on that too. Sorry for the mess ahead of time.

import java.util.Arrays;

//I don't have my class or main method posted to save room ;)

public static void ArrayPrinter(String[] transactions)
    {
        System.out.println(transactions);
    }
    
    public String[] TransactionName()
    {
        String[] transactions = new String[]{"Mr. ", "Mrs. ", "Ms. "};
        ArrayPrinter(transactions);
    }

How can I make it compile?

TobiSH
  • 2,833
  • 3
  • 23
  • 33
trymay
  • 1
  • 1
  • Change return type of `TransactionName()` method to `void`, then it should compile and run fine. Assuming you put the methods inside a class, of course, since you can't have methods outside of classes. --- Also read: [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784/5221149) – Andreas Aug 28 '20 at 03:37
  • This looks like homework. – Stefan Steinegger Aug 28 '20 at 06:12

1 Answers1

0

You need to instantiate the class inside the static method to call non static method

    public class Test {
    
        public static void main(String[] args) {
        String[] transactions = new String[]{"Mr. ", "Mrs. ", "Ms. "};
        ArrayPrinter(transactions);
        }
    
        public static void ArrayPrinter(String[] transactions) {
            Test t = new Test();
            for(String s : t.TransactionName()) {
            System.out.println(s);
        }
        }
    
        public String[] TransactionName() {
            String[] transactions = new String[] { "Mr. ", "Mrs. ", "Ms. " };
    
            return transactions;
        }
    
    }

The above code will call the nonstatic method and outputs following - Mr. Mrs. Ms.