Main method that calls two method and passes result of calling first method over to second method

Viewed 391

I'm tasked to create a main method that calls two methods. The first method returns array of strings while the second method takes the array of strings and print out the element on separate lines. the main method then passes the result of calling the first method to the second and then halt. Am i doing it understanding the question correctly? When i compile and execute i get

sunshine
road
73
11

public class Hihihi
{

public static void main(String args[]) 
{
  method1();
  method2();//Will print the strings in the array that
            //was returned from the method1()
   System.exit(0); 
}                                 



public static String[] method1() 
{
     String[] xs = new String[] {"sunshine","road","73","11"};
     String[] test = new String[4];
     test[0]=xs[0];
     test[1]=xs[1];
     test[2]=xs[2];
     test[3]=xs[3];
     return test;
 }

   public static void  method2()
  { 
    String[] test = method1();
    for(String str : test)
        System.out.println(str); 
  } 
}
5 Answers

Your code works, but in main, the return value of method1 is ignored. And calling method1 in method2 is unecessary.

You can let method2 accept parameter String[] strings:

public static void method2(String[] strings) {

    for (String str : strings)
        System.out.println(str);
}

and pass the result of method1 to method2:

String[] result = method1();
method2(result);//Will print the strings in the array that

At first, correct your method2

It has to be able to accept an array of String elements as parameter:

public static void  method2(String[] test)
  { 
    // this line is not needed -> String[] test = method1();
    for(String str : test)
        System.out.println(str); 
  } 

This way, you actually pass the data to the method, as your requirements described. A bonus is: it's re-usable for other String arrays as well.

Your method1 has got a lot of redundant code. Just filter that out

public static String[] method1() 
{
     return new String[] {"sunshine","road","73","11"};
}

and now, your main method, just to link them. Change

public static void main(String args[]) 
{
  method1();
  method2(); // this will now cause a compilation error, because it expects a parameter
   System.exit(0); 
} 

to:

public static void main(String args[]) 
{      
  method2(method1());
   System.exit(0); 
} 

The way your code was originally built, method1 was called two times, first in the main method, which was completely redundant, since the result wasn't used, second time in the method2, where it shouldn't be called, since the data should be passed as a parameter.

If you want that the data "flows" trough main as you should do this:

public static void main(String args[]){
      String[] arr = method1();
      method2(arr);
      System.exit(0); 
}   

public static String[] method1(){
     String[] xs = new String[] {"sunshine","road","73","11"};
     String[] test = new String[4];
     test[0]=xs[0];
     test[1]=xs[1];
     test[2]=xs[2];
     test[3]=xs[3];
     return test;
}

public static void  method2(String[] arr){ 
    for(String str : arr){
        System.out.println(str); 
    } 
}

Proper way of doing the above is like below.You have to take the return value of the method one and insert it into method two as a parameter.

 public class Hihihi
    {

    public static void main(String args[]) 
    {
      String[] test=method1();
      method2(test);
    }                                 



    public static String[] method1() 
    {
         String[] xs = new String[] {"sunshine","road","73","11"};
         String[] test = new String[4];
         test[0]=xs[0];
         test[1]=xs[1];
         test[2]=xs[2];
         test[3]=xs[3];
         return test;
     }

       public static void  method2(String[] newtest)
      { 

        for(String str : newtest)
            System.out.println(str); 
      } 
    }

You are almost there: the return of your method1() should be used in the first call (you are actually ignoring it), and you have to use that return value (I've made some notes in the code aswell):

public static void main(String args[]) {
  String[] result = method1();  //TAKE THE VALUE HERE
  method2(result);//pass the result here as parameter
  System.exit(0); //there's no need of this, the program will exit when method2 is finished
}  

So edit your method2 for getting the result of method1 as parameter

public static void  method2(String[] arrayToPrint){ 
    for(String str : arrayToPrint)
        System.out.println(str); 
} 
Related