How to solve the problem "The main method was not found" in a class in Java?

4

I created this ArrayList and at the time of execution the following message appears:

  

Error: The main method was not found in class   DeclarationArray.Declaracao_Array; set the parent method as:   public static void main (String [] args)

Reading the message, and with the help of colleagues here in the SOPT, it is noticed that my code is missing the main method, my question is Where do I put this method? I tried in many ways, but without success.

How would my code be with this new method?

package DeclaracaoArray;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public String sorteia (){

            List<String> lista = new ArrayList<String>();

            lista.add ( "Alice" );
            lista.add ( "Bruno" );
            lista.add ( "Carlos");
            lista.add ( "Daniel");

           Collections.shuffle ( lista );   

           return lista.get(0);
        }
    }
    
asked by anonymous 29.09.2015 / 15:44

3 answers

4

This code can go anywhere within your class, for example like this:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args) {
        Declaracao_Array d = new Declaracao_Array();
        System.out.println(d.sorteia());
    }

    public String sorteia (){
        List<String> lista = new ArrayList<String>();

        lista.add ( "Alice" );
        lista.add ( "Bruno" );
        lista.add ( "Carlos");
        lista.add ( "Daniel");

        Collections.shuffle ( lista );   

        return lista.get(0);
    }
}

For the above case I created an object of the type of the class itself so that it was possible to call the method sorteia() otherwise it would not be accessible, but you still have the option to do otherwise by setting the method to static and access it directly from the main method without the need to instantiate an object. So:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args) {
        System.out.println(sorteia());
    }

    public static String sorteia (){
        List<String> lista = new ArrayList<String>();

        lista.add ( "Alice" );
        lista.add ( "Bruno" );
        lista.add ( "Carlos");
        lista.add ( "Daniel");

        Collections.shuffle ( lista );   

        return lista.get(0);
    }
}
    
29.09.2015 / 15:47
3

You need to have a method with the following signature

public static void main(String[] args)

This will be the entry point for your application. It will be the first method to run and so is required.

Applying to your code would look something like:

package DeclaracaoArray;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args){
        sorteia();
    }       

    public static String sorteia (){

        List<String> lista = new ArrayList<String>();

        lista.add ( "Alice" );
        lista.add ( "Bruno" );
        lista.add ( "Carlos");
        lista.add ( "Daniel");
        Collections.shuffle ( lista );   

        return lista.get(0);
    }
}
    
29.09.2015 / 15:47
3

Example:

public class Declaracao_Array {

     public static void main(String[] args) {

     }

     public static void metodo(){

     }

} 
    
29.09.2015 / 15:48