How to record a txt file with each patient’s name?

Asked

Viewed 681 times

1

I’m developing a college project which is a clinic system where the receptionist makes the patient’s registration and other more options. I’m trying to get the employee to register a new patient, save the form with the patient’s name individually. For each patient a new txt form is saved. The code I am doing, it is saving only in the file called Report, and the way it is it will always overwrite the previous one. How I make the individual records with the name of each patient?:

 public static void main(String[] args) throws IOException {
Scanner ler = new Scanner(System.in);
String nomeArq="Relatorio.pdf";
int i;
String nome;
String ender;
String email;
String tel;
String bairro;
String numero;
String exame;

File arquivo; 
System.out.printf("Nome do paciente: ");
nome = ler.next();
System.out.printf("Endereço: ");
ender = ler.next();
System.out.printf("Bairro: ");
bairro = ler.next();
System.out.printf("Número: ");
numero = ler.next();
System.out.printf("Telefone: ");
tel = ler.next();
System.out.printf("E-mail: ");
email = ler.next();
System.out.printf("Exame: ");
exame = ler.next();

try
{
  Formatter saida = new Formatter(nomeArq);
  saida.format("          --- Ficha cadastral de pacientes ---");
  saida.format('\n'+"Nome do paciente: "+nome);
  saida.format('\n'+"Endereço: "+ender);
  saida.format('\n'+"Bairro: "+bairro);
  saida.format('\n'+"Numero: "+numero);
  saida.format('\n'+"Email: "+email);
  saida.format('\n'+"Telefone: "+tel);
  saida.format('\n'+"Exame: "+exame);
  saida.close();
    System.out.println("Arquivo '"+nomeArq+"' Salvo com sucesso!");
}
//mostrando erro em caso se nao for possivel gerar arquivo
catch(Exception erro){
  System.out.println("Arquivo nao pode ser gerado!");
}
  • Your question says something the code is doing wrong, but it’s not even doing it. It would be better to complete the recording part of the file and then try to see why it is recording the wrong name as described. And the solution is simple: use the patient name as the file name. It is a naive solution, but it works and all the architecture of this code is already very naive.

  • .txt with the patient’s name? What if you have two patients with the same name? It would not be better to save this data in a database and generate a report from it?

  • Guys, the work can not have BD and in case of having two or more patients with the same name, the idea is to save with the name and surname of the user. In the above code it saves the form with Tdas the user information put the name of the file ta Report. Being that I want the name and surname of each. example: Maria Jose.pdf

  • Changes the line Formatter saida = new Formatter(nomeArq); para Formatter saida = new Formatter(nome +".pdf"); Or better before that line changes the nomeArq = nome +".pdf";

2 answers

4

Enter the code String nomeArq="Relatorio.pdf"; just before the try. But already define the name you will want, in case...

String nomeArq = "Relatorio-" + nome + ".txt";
try
{

2


Just change the variable nameArq with the name you want.

public static void main(String[] args) throws IOException {
Scanner ler = new Scanner(System.in);
String nomeArq="Relatorio.pdf";
int i;
String nome;
String ender;
String email;
String tel;
String bairro;
String numero;
String exame;

File arquivo; 
System.out.printf("Nome do paciente: ");
nome = ler.next();
System.out.printf("Endereço: ");
ender = ler.next();
System.out.printf("Bairro: ");
bairro = ler.next();
System.out.printf("Número: ");
numero = ler.next();
System.out.printf("Telefone: ");
tel = ler.next();
System.out.printf("E-mail: ");
email = ler.next();
System.out.printf("Exame: ");
exame = ler.next();


nomeArq = nome +".pdf"; // aqui banta altera para o nome que você queira. 

try
{
  Formatter saida = new Formatter(nomeArq);
  saida.format("          --- Ficha cadastral de pacientes ---");
  saida.format('\n'+"Nome do paciente: "+nome);
  saida.format('\n'+"Endereço: "+ender);
  saida.format('\n'+"Bairro: "+bairro);
  saida.format('\n'+"Numero: "+numero);
  saida.format('\n'+"Email: "+email);
  saida.format('\n'+"Telefone: "+tel);
  saida.format('\n'+"Exame: "+exame);
  saida.close();
    System.out.println("Arquivo '"+nomeArq+"' Salvo com sucesso!");
}
//mostrando erro em caso se nao for possivel gerar arquivo
catch(Exception erro){
  System.out.println("Arquivo nao pode ser gerado!");
}

Browser other questions tagged

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