John P. John P. - 27 days ago 5
Java Question

Is there a way to use the existing variables in a random array method in another method?

I want to use the variables generated in my

randomRainMonth()
method to be used for calculation in another method
totalRain()
. But by using
double[] totalsum = randomRainMonth(list);
in my
totalRain()
it would just generate a new set of random variables as it would invoke the method again. Can anyone help?

public class RainFall{

public static void main (String[] args){

double [] monthRain = new double[12];
String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
randomRainMonth(monthRain);
System.out.println("-------");
totalRain(monthRain, monthName);
// printMonths(monthName);
}

public static String[] printMonths(String list[]){
String[] pM = new String[list.length];
for (int i = 0; i<list.length; i++) {
//System.out.println(list[i]);
}
return pM;
}

public static double[] randomRainMonth(double list[]){
double[] rdMonth = new double[list.length];

for(int i = 0; i < list.length; i++){
rdMonth[i] = (int)(Math.random() * 100);
//System.out.println(rdMonth[i]);
}
return rdMonth;
}

public static double[] totalRain(double list[], String list2 []){

String[] mnth = printMonths(list2);
double[] totalsum = randomRainMonth(list);

double sum = 0;
for( double i : totalsum) {
sum += i;
}


for (int i = 0; i<list2.length; i++) {

System.out.print(mnth[i] +": ");
System.out.println(totalsum[i]);
}
System.out.println("Total: ");
System.out.println(sum);

return totalsum;

}
}


[SOLVED]
Final code:

public class RainFall{

public static void main (String[] args){


double[] monthRain = randomRainMonth();
String[] monthName = printMonths();
// randomRainMonth();
System.out.println("-------");
totalRain(monthRain, monthName);
// printMonths(monthName);
avgRainMonthly(monthRain);
}

public static String[] printMonths(){
String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (int i = 0; i<12; i++) {
//System.out.println(monthName[i]);
}
return monthName;
}

public static double[] randomRainMonth(){

double [] monthRain = new double[12];
for(int i = 0; i < 12; i++){
monthRain[i] = (int)(Math.random() * 100);
//System.out.println(monthRain[i]);
}
return monthRain;
}

public static double[] totalRain(double list[], String list2 []){

String[] mnth = list2;
double[] totalsum = list;

double sum = 0;
for( double i : totalsum) {
sum += i;
}


for (int i = 0; i<12; i++) {

System.out.print(mnth[i] +": ");
System.out.println(totalsum[i]);
}
System.out.println("Total: ");
System.out.println(sum);

return totalsum;

}

public static double[] avgRainMonthly(double list []){

double[] average = list;

double avg = 0;
double finAvg = 0;
for( double a : average) {
avg = avg + a;
finAvg = avg / 12;
}
System.out.println(finAvg);
return average;
}
}

Answer

You shouldn't create double [] monthRain = new double[12]; ahead of the randomRainMonth(monthRain); method call, because the actual array object needs to be created and returned by the method randomRainMonth.

Rather, you need to declare a double[] array monthRain in your main and then collect the result returned by the randomRainMonth(monthRain) as below:

//declare the double array
double[] monthRain = randomRainMonth();

randomRainMonth() method changed as below:

public static double[] randomRainMonth(){
      //this method should be responsible to create the double array object 
      //and then return it to the caller after populating with elements
      double [] monthRain = new double[12];

      for(int i = 0; i < list.length; i++){
        rdMonth[i] = (int)(Math.random() * 100);
        //System.out.println(rdMonth[i]);
      }
      return monthRain;
  }

Now, you can pass the collected array (returned by randomRainMonth) to the next method as below (which u are already doing):

totalRain(monthRain, monthName);
Comments