Problem when registering user in the list

Asked

Viewed 69 times

0

I’m trying to register users and put them on the list. Initially I just tried to register and test with print. (Still indicates errors) Does anyone know why it’s not working? The mistakes are:

cannot find Symbol on the client.setName(scanner.nextLine());

and

Constructor Clients in class Customers cannot be Applied to Given types;
required: java.lang.String
found: no Arguments
Reason: actual and formal Arguments lists differ in length

Code:

    import java.util.ArrayList;
    import java.util.Scanner;
    public class CadastraUsuarios {

    public void menu(){
    Scanner entrada = new Scanner(System.in);
    ArrayList<Clientes> listaDeUsuarios = new ArrayList<Clientes>();
     Clientes cliente = new Clientes();      
    int op = 0;


    char opcao;

    System.out.printf("MENU DE ESCOLHAS");
    System.out.printf("A- CADASTRAR CLIENTE");
    System.out.printf("B- INSERIR O CADASTRO DE UM NOVO TIPO DE SEGURO");
    System.out.printf("C- LISTAR TODOS OS SEGUROS CADASTRADOS");
    System.out.printf("D- LISTAR TODOS OS CLIENTES CADASTRADOS");
    System.out.printf("E- LISTAR OS SEGUROS CADASTRADOS PARA UM DETERMINADO CLIENTE");
    System.out.printf("F- BUSCAR INFORMAÇÕES DE UM SEGURO PELO NÚMERO DE ÁPOLICE");
    System.out.printf("S- SAIR");

    switch(opcao){
        case 'a': System.out.printf("Cadastro de Cliente:");

        //cadastrar clientes
        break;
        case 'b': System.out.printf("");
        //inserir o cadastro de um novo tipo de seguro
        break;
        case 'c' : System.out.printf("");
        //listar todos os seguros cadastrados

        break;
        case 'd' : System.out.printf("");
        //listar todos os clientes cadastrados

        break;
        case 'e' : System.out.printf("");
        //listar os seguros cadastrados para um determinado cliente

        break;
        case 'f' : System.out.printf("");
        //buscar as informações de um seguro pelo número de apólice

        break;
        case 's' : System.out.printf("saindo do programa");
        //sair do programa

        break;



    }
    }

    public  void cadastro(){
    System.out.println("Bem vindo ao sistema de cadastros de usuários");
    System.out.println("Digite o nome do usuário");
    cliente.setNome(scanner.nextLine());
    System.out.println(cliente.getNome());
    }
    }

Client class:

 public class Clientes
 {
 String nome; 
 String cpf; 
 String data_de_nascimento;
 String endereco;

 public Clientes(String nome)
 {
    this.nome = nome ;
    System.out.println("Construtor Funcionario");
}
    public void setNome(String nome) {
    this.nome = nome;
}
public String getNome()
{
    return nome;
}

}
  • what errors are indicated?

  • really, what’s the mistake?

  • cannot find Symbol on the client.setNome(scanner.nextLine(); e Constructor Clients in class Clients cannot be Applied to Given types; required: java.lang.String found: no Arguments Reason: actual and formal Arguments lists differ in length

1 answer

3


The variable input is within the scope CadastraUsuarios.menu:

CadastraUsuarios {

    public void menu(){
    Scanner entrada = new Scanner(System.in);

There is no way to have access in a totally different scope than in the case is CadastraUsuarios.cadastro:

Besides, you changed the name entrada for scanner

public  void cadastro(){
System.out.println("Bem vindo ao sistema de cadastros de usuários");
System.out.println("Digite o nome do usuário");
cliente.setNome(scanner.nextLine());
System.out.println(cliente.getNome());
}

The right thing would be something like:

public class CadastraUsuarios
{
    Scanner entrada;

    public void menu()
    {
       entrada = new Scanner(System.in);
       ...
    }

    public  void cadastro(){
       ...
       cliente.setNome(entrada.nextLine());
       ...
    }

Then it will be accessible "in the scope" of the whole class

Another problem cited by @LINQ is that the Customers class waits in the builder for a String:

public Clientes(String nome)
 {
    this.nome = nome ;
    System.out.println("Construtor Funcionario");
}

But when instantiating you passed nothing:

ArrayList<Clientes> listaDeUsuarios = new ArrayList<Clientes>();
Clientes cliente = new Clientes();  //AQUI

For public Clientes(String nome) is expected to pass a String, as nothing happened it causes the mistake:

cannot be Applied to Given types; required: java.lang.String found: no Arguments Reason: actual and formal Arguments lists differ in length

Browser other questions tagged

You are not signed in. Login or sign up in order to post.