not suitable method found for add (Java)

1

I am trying to create a small program that generates random bets to the Quiniela. It gives me error in
 listaquiniela.add (object.generarapuesta ());

Quiniela class

import java.util.*;

public class Quiniela {
    ArrayList < Apuesta > listaquiniela;

    public Quiniela() {
        listaquiniela = new ArrayList<Apuesta>();
    }

    public ArrayList<Apuesta> generarquiniela (int numero) {
        Apuesta objeto = new Apuesta();

        for (int i=0; i<numero; i++) {
            listaquiniela.add(objeto.generarapuesta());
        }
        return listaquiniela;
    }

    public void mostrarquiniela (){
        Apuesta objeto = new objeto();

        for (int i=0; i<listaquiniela.size(); i++){
                System.out.println(listaquiniela.get(i));
            }
    }
}

Main Class

import java.util.*;

public class Principal{
    public static void main (String [] args){
        int numero = 0;
        String con = "S";
        Scanner escaner = new Scanner(System.in);
        do{
            numero = escaner.nextInt();
            while (numero<2 || numero>8){
                System.out.println("Elija el número de apuestas");
                System.out.println("El mínimo son 2 y el máximo 8");
                numero = escaner.nextInt();
            }
            Quiniela apuesta = new Quiniela();
            apuesta.generarquiniela(numero);
            System.out.println("¿Desea realizar más apuestas?");
            System.out.println("S/N");
            con = escaner.nextLine();
        }while (con.equals("S"));
    }
}

Betting Class

import java.util.*;

public class Apuesta {
    ArrayList<String>listaapuesta;

    public Apuesta(){
        listaapuesta=new ArrayList<String>();
        for (int i=0; i<15; i++){
            listaapuesta.add(" ");
        }
    }

    public ArrayList<String> generarapuesta(){
        int v=0;
        Random objeto = new Random();

        for (int i=0; i<listaapuesta.size(); i++){
            v=objeto.nextInt(3);

            if(v==0){
                listaapuesta.set(i, "x");
            }else{
                listaapuesta.set(i, v+"");
            }
        } return listaapuesta;
    }

    public String mostrarapuesta(){
        String resultado = "";
        for (int i=0; i<listaapuesta.size(); i++){
            resultado = resultado +" " +listaapuesta.get(i);

        }return resultado;
    }
}
    
asked by Ele 22.05.2017 в 15:23
source

4 answers

1

As I put you in the comments, the error message is caused because you can not find any implementation of the generarapuesta method within your class Apuesta that fits the input values ( void ) and output ( Apuesta ).

The way in which you implement the creation of Apuesta s and the guards is incorrect. You should do it in the following way:

public ArrayList<Apuesta> generarquiniela (int numero) {
    for (int i=0; i<numero; i++) {
        /* Por cada quiniela rellena creo una instancia de "Apuesta" */
        Apuesta objeto = new Apuesta();
        /* Genero la apuesta */
        objeto.generarapuesta();
        /* Guardo la apuesta en nuestra lista de apuestas */
        listaquiniela.add(objeto);
    }
    /* Esto es innecesario, pero te lo mantengo por ahora */
    return listaquiniela;
}

At the end of generarquiniela execution, n bets have been saved in the listaquiniela list.

But as I put the code, you will need a small modification in the class Apuesta .

Instead of:

public ArrayList<String> generarapuesta(){

Use:

public void generarapuesta(){

We do not need to return the bet because it is saved internally in the instance, in the listaapuesta property.

On the other hand, we should also fix mostrarquiniela in the following way:

public void mostrarquiniela (){
    /* Por cada apuesta de la lista llamamos a su método "mostrarapuesta" */
    for (int i=0; i<listaquiniela.size(); i++){
            System.out.println("Quiniela Nº" + i);
            System.out.println(listaquiniela.get(i).mostrarapuesta());
        }
}

And, finally, you have to adapt Principal so that the text that asks for the number of bets comes out before asking you the number and that after generating the betting the content is shown:

import java.util.*;

public class Principal{
    public static void main (String [] args){
        int numero = 0;
        String con = "S";
        Scanner escaner = new Scanner(System.in);
        do{
            do {
                System.out.println("Elija el número de apuestas");
                System.out.println("El mínimo son 2 y el máximo 8");
                numero = escaner.nextInt();
            } while (numero < 2 || numero > 8);
            Quiniela apuesta = new Quiniela();
            /* Generamos "numero" apuestas */
            apuesta.generarquiniela(numero);
            /* Las mostramos */
            apuesta.mostrarquiniela();
            System.out.println("¿Desea realizar más apuestas?");
            System.out.println("S/N");
            con = escaner.nextLine();
        }while (con.equals("S"));
    }
}
    
answered by 22.05.2017 / 15:56
source
1

Your method generarapuesta() returns a ArrayList of String and you're passing it to ArrayList of Apuesta

    
answered by 22.05.2017 в 15:27
1

It's a problem of types.

generateAttribute () returns a String ArrayList and you are trying to add it to a Bet ArrayList that is of a different type.

Simply define listQuiniela as ArrayList

ArrayList < String> listaquiniela;

and use addAll instead of all.

listaquiniela.addAll(objeto.generarapuesta());
    
answered by 22.05.2017 в 15:31
0

Your error is that you try to match an ArrayList to an ArrayList and it is NOT the same.

listaquiniela.add(objeto.generarapuesta());//Esto está mal

listquiniela can only be equal to an ArrayList. Just as you can not match any type of object to another that is not the same type. Ex: int i = "hola mundo!";

    
answered by 22.05.2017 в 15:33