How to set instance variable using the read value of a Scanner?

Asked

Viewed 129 times

1

To declare the value of a class instance variable Paciente by class Main with the Scanner in Java?

Codes:

public class Paciente{

  private double cod;
  private String nome;

  public getNome(){
      return this.nome;
  }

  public void setNome(String NomedoPaciente){
      this.nome = NomedoPaciente;
  }
}

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner input = new Sacanne(System.in);
        Paciente p01 = new Paciente();

        System.out.printlm("Digite o nome do Paciente: ");
        String nomedopaciente = input.nextLine();
        p01.setNome(nomedoPaciente);
    }
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

This code has a huge amount of typing errors (and des organization that does not prevent the compilation) and this is what is preventing it to work, but I decided to answer (although it may be innocuous) because the code has several conceptual errors.

Almost every class should have one builder that makes sense and initializes an object in a valid state.

Almost always the use of getters are unnecessary or inappropriate and should give preference to methods that do something more meaningful and setters make less sense because in general most of the data should not be changed, but if it can be then it should be by a method that has more semantics. Not to mention that in many cases accessing the direct field would be simpler and easier, just know what you are doing. Understand more the subject following links in another question.

I would need to create a mechanism to control the code that I switched to whole because it doesn’t make any sense to have one double.

Object-oriented programming is to model objects correctly, with the right concepts, otherwise you don’t need this complexity.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Digite o nome do Paciente: ");
        Paciente p01 = new Paciente(1, input.nextLine());
    }
}

class Paciente {
    private int codigo;
    private String nome;
    public Paciente(int codigo, String nome) {
        this.codigo = codigo;
        this.nome = nome;
    }
    public String getNome() { return nome; }
    public void setNome(String nome) { nome = this.nome; }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • thank you very much, forgive me for the mistakes I’m studying programming since January this year and I started at zero even then I still have difficulties because I was without internet, everything I know was through books and handouts but nothing better than someone with experience to get up and help, I am humbly grateful brother...

Browser other questions tagged

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