-2

This is the code I'm using.

import java.util.ArrayList; public class Random {

public static ArrayList<Integer> generateRandomList( int size, int min, int max) {
    ArrayList<Integer> list;
    list = new ArrayList<>();
    for(int i=0;i<size;i++) {
        int n = (int)(Math.random() * (max-min))+min;
        list.add(n);
    }
    return list;
}

}

In Eclipse IDE when I try to run it says "the selection cannot be launched, and there are no recent launches." In Netbeans it says "no main classes found."

What am I doing wrong?

JavaNub
  • 13
  • 4
  • 2
    I don't see a main class either. Where is your `public static void main(String[] argv)`? – Thilo Mar 13 '21 at 02:24
  • By the way, no need to write such a method. `List < Integer > integers = ThreadLocalRandom.current().ints( 3 , 1 , 100 ).boxed().toList();` in Java 16 and later. Make the ending `.collect( Collectors.toList())` in earlier Java. – Basil Bourque Mar 13 '21 at 02:41

1 Answers1

1

In java, you need a main method to run. In the code you have included, that isn't there, and so the IDE doesn't know what you want to do with it. When I use the following code, it works as expected:

import java.util.ArrayList;

public class Random {

public static void main(String[] args)
{
    System.out.println(generateRandomList(3,0,5));
}

public static ArrayList<Integer> generateRandomList( int size, int min, int max) {
    ArrayList<Integer> list;
    list = new ArrayList<>();
    for(int i=0;i<size;i++) {
        int n = (int)(Math.random() * (max-min))+min;
        list.add(n);
    }
    return list;
}

}

Here is a link to a good explanation about main() methods in java.

Hawkeye5450
  • 672
  • 6
  • 18