1

Read the end, here at the beginning is the code:

class:

package Relatorio;

public class Cliente {
    private String nome;
    private String sexo;
    private int idade;
    private double salario;

    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getSexo() {
        return sexo;
    }
    public void setSexo(String sexo) {
        this.sexo = sexo;
    }
    public int getIdade() {
        return idade;
    }
    public void setIdade(int idade) {
        this.idade = idade;
    }
    public double getSalario() {
        return salario;
    }
    public void setSalario(double salario) {
        this.salario = salario;
    }
}

class:

package Relatorio;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class Leitor {

    Charset utf8 = StandardCharsets.UTF_8;
    Path path = null;
    String line = null;

    public ArrayList<String> ler(String url) {
        path = Paths.get(url);
        ArrayList<String> lines = null;
        try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
            lines = new ArrayList<>();
            while((line = reader.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    public ArrayList<Object> lerRelatorio(String url, Relatorio relatorio) {
        path = Paths.get(url);
        ArrayList<Object> lines = null;
        try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
            lines = new ArrayList<>();
            while((line = reader.readLine()) != null) {
                lines.add(relatorio.tipoDoRelatorio(line));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }
}

class:

package Relatorio;

import java.util.ArrayList;

import Praticando.Cliente;

public abstract class Relatorio {

    public abstract Cliente tipoDoRelatorio(String line);

    public abstract void apresentarRelatorio(ArrayList<Cliente> listaClientes);

}

class:

package Relatorio;

import java.util.ArrayList;

import Praticando.Cliente;

public class RelatorioBasico extends Relatorio {

    Cliente cliente = null;
    int aux = 0;

    @Override
    public Cliente tipoDoRelatorio(String line) {
        if(aux == 0) {
            cliente = new Cliente();
            cliente.setNome(line);
        }
        if(aux == 1) {
            cliente.setSexo(line);
        }
        if(aux == 2) {
            cliente.setIdade(Integer.parseInt(line));
        }
        if(aux == 3) {
            cliente.setSalario(Double.parseDouble(line));
            aux = -1;
        }
        aux++;
        return cliente;
    }

    @Override
    public void apresentarRelatorio(ArrayList<Cliente> listaClientes) {
        for(Cliente cli : listaClientes) {
            System.out.println("Nome: " + cli.getNome() + ", tem " + cli.getIdade() + " anos e é do sexo " + cli.getSexo() + ".");
        }
    }

}

class:

package Relatorio;

public class AppTeste {

    public static void main(String[] args) {

        Leitor leitor = new Leitor();
        RelatorioBasico relatorio = new RelatorioBasico();

        relatorio.apresentarRelatorio(leitor.lerRelatorio("C:/arquivo.txt", relatorio));
    }

}

error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method apresentarRelatorio(ArrayList<Cliente>) in the type RelatorioBasico is not applicable for the arguments (ArrayList<Object>)

    at Relatorio.AppTeste.main(AppTeste.java:10)

seemed to be all right, even when I went to test in the AppTeste class, when I declared:

relatorio.apresentarRelatorio(leitor.lerRelatorio("C:/arquivo.txt", relatorio));

already appeared an alert saying:

The method apresentarRelatorio(ArrayList<Cliente>) in the type RelatorioBasico is not applicable for the arguments (ArrayList<Object>)

I thought it might be because of the method in the Reader class:

public ArrayList<Object> lerRelatorio(String url, Relatorio relatorio) {
        path = Paths.get(url);
        ArrayList<Object> lines = null;
        try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
            lines = new ArrayList<>();
            while((line = reader.readLine()) != null) {
                lines.add(relatorio.tipoDoRelatorio(line));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

the parts:

public ArrayList<Object> lerRelatorio(String url, Relatorio relatorio) {

ArrayList<Object> lines = null;

because of the ArrayList, but even being specific as ArrayList, it keeps going wrong, I've read and reread it many times and I do not know what it can be and where I'm wrong ...

Please, if anyone realized the mistake could you tell me?

if anyone needs the source, link: https://github.com/frnndio/Praticando/tree/master/PraticandoJava/src/Relatorio

Fernando
  • 13
  • 5
  • 1
    replace all occurrences `ArrayList` with `ArrayList` in `lerRelatorio` function – Iłya Bursov May 11 '18 at 04:17
  • You cannot pass a `ArrayList` to `ArrayList< Cliente>` – Thiyagu May 11 '18 at 04:18
  • Possible duplicate of [Is List a subclass of List? Why are Java generics not implicitly polymorphic?](https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-are-java-generics-not-implicitly-po) – Logan May 11 '18 at 04:18
  • this is the first thing I did, it replaces: `ArrayList` by `ArrayList`, but it still has a problem, I also thought it might be this, but it's something else, so I came to ask, but thank you right away – Fernando May 11 '18 at 04:21
  • when I change to: `ArrayList` the following appears in the `add` of `lines.add (report.TypeList (line));` method `readReport` : `The method add (Report.Client) in the type ArrayList is not applicable for the arguments (Practicing.Customer)` – Fernando May 11 '18 at 04:34

2 Answers2

1

This error indicates that the method apresentarRelatorio(ArrayList<Cliente>) wants an ArrayList<Cliente> but you are calling it with an ArrayList<Object>.

You have correctly identified the lines that need to be changed:

public ArrayList<Object> lerRelatorio(String url, Relatorio relatorio) {

Should be

public ArrayList<Cliente> lerRelatorio(String url, Relatorio relatorio) {

And

ArrayList<Object> lines = null;

Should be

ArrayList<Cliente> lines = null;

Edit: Additionally, you have two copies of your Cliente class in your code, one in Praticicando and the other in Relatorio. RelatorioBasico.apresentarRelatorio() expects Praticando.Cliente instances but Leitor.lerRelatorio() creates Relatorio.Cliente instances.

line 5 of both Relatorio.java and RelatorioBasic.java both import Practicando.Cliente, which is the cause of that error:

import Practicando.Cliente;

Delete those 2 import statements, and your code will compile.

Cosmo
  • 144
  • 6
  • ty, but when I change to: `ArrayList` the following appears in the `add` of `lines.add (report.TypeList (line));` method `readReport` : `The method add (Report.Client) in the type ArrayList is not applicable for the arguments (Practicing.Customer)` – Fernando May 11 '18 at 04:31
  • See my edit, the class is `Cliente` not `Client` (note: please don't bother translating the classnames to English for discussiion, it's confusing if anything, Spanish is similiar/familiar enough to me that I the general meaning) – Cosmo May 11 '18 at 04:32
  • yes, I put `Cliente`, in the comment I wrote wrong, sorry, edited and corrected – Fernando May 11 '18 at 04:33
  • when I substitute `ArrayList` for `ArrayList`, now in lines: 10 in class `AppTest` and 38 in class `Leitor` – Fernando May 11 '18 at 04:50
  • but they are in different packages, one does not interfere in the other, disregard the package Praticando for kindness, but thx – Fernando May 11 '18 at 04:55
  • In line 5 of `RelatorioBasic.java` you have imported `Praticando.Cliente` by accident, then. – Cosmo May 11 '18 at 04:58
  • Woooow, THX, I was getting an import from the Praticando package, thank you very much, it helped a lot of your comment, my friend, thank you very much – Fernando May 11 '18 at 05:06
  • No worries. I've updated my answer to reflect this. Please accept my answer so that others will know the question has been resolved. – Cosmo May 11 '18 at 05:08
0

The problem is in your RelatorioBasico class,

apresentarRelatorio() get the argument as ArrayList<Cliente>

and Leitor class , lerRelatorio() return type is ArrayList<Object> .

so in your AppTeste class you write

relatorio.apresentarRelatorio(leitor.lerRelatorio("C:/arquivo.txt", relatorio));

so relatorio.apresentarRelatorio() get argument as ArrayList<Object> not ArrayList<Cliente>.

Solution is you can change

lerRelatorio() which returns ArrayList<Cliente>

Do changes for Leitor class , lerRelatorio() as follows.

public ArrayList<Cliente> lerRelatorio(String url, Relatorio relatorio) {
        path = Paths.get(url);
        ArrayList<Cliente> lines = null;
        try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
            lines = new ArrayList<Cliente>();
            while((line = reader.readLine()) != null) {
                lines.add(relatorio.tipoDoRelatorio(line));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }
MinA
  • 405
  • 4
  • 9
  • ty, but when I change to: `ArrayList` the following appears in the `add` of `lines.add (report.TypeList (line));` method `readReport` : `The method add (Report.Client) in the type ArrayList is not applicable for the arguments (Practicing.Customer)` – Fernando May 11 '18 at 04:59