0

I've seen similar question appear on the forum but they didn't work or I implemented them wrong.

Error:

The method randomFill() is undefined for the type Generatoraufgabe?

My code:

import java.util.Random;

public class Generatoraufgabe {

 static void generators() {

    Random r= new Random(9);
    int n ;
    int i ;
    int intArray[] ;
    for(i=0 ; i <= 10 ; i++) {

        intArray[i]=randomFill() ;      
    }
    system.out.println(intArray(i));
  }
}
Abdel-Raouf
  • 700
  • 1
  • 8
  • 20
dsisko
  • 15
  • 3
  • When are you using r?. Also, your println() should have intArray[], not inrArray. From the error it seems that randomFill function is the problem – darkhouse Oct 05 '19 at 18:38
  • Possible Duplicate: https://stackoverflow.com/questions/20380991/fill-an-array-with-random-numbers – Abdel-Raouf Oct 05 '19 at 18:54
  • Your method `randomFill()` is `undefined` because you call a function that doesn't exist yet. – Abdel-Raouf Oct 05 '19 at 18:57

2 Answers2

1

you do not need Random Object in your generator method, you do not need n variable, your array is not initialized, you system out array that does not exist. you do not have random Fill method. try this code.

static void generators() {
    int i;
    int[] intArray = new int[10];
    for (i = 0; i <10; i++) {

        intArray[i] = randomFill();
    }
    System.out.println(Arrays.toString(intArray));
}

private static int randomFill() {

    return new Random().nextInt(100);
}
George Weekson
  • 483
  • 3
  • 13
0

If you are wishing to generate 9 random integers, you can use the following code:

import java.util.Random;
public class Example {
   public static void main(String[] args) {
      Random rd = new Random(); // creating Random object
      int[] arr = new int[9];
      for (int i = 0; i < arr.length; i++) {
         arr[i] = rd.nextInt(); // storing random integers in an array
         System.out.println(arr[i]); // printing each array element
      }
   }
}
Ido Segal
  • 430
  • 2
  • 7
  • 20