2016-04-24 6 views
-3

CSVファイルから情報を印刷する必要があります。問題は、オブジェクトを "bean"の属性にフォーマットする責任があるParserクラスを使用する必要があることです。私はそれを実行しようとすると、CSVファイルにある情報の代わりに、アドレスを出力します。私がそれをするために使っているクラスは以下の通りです。私はこの問題がメソッド "public T readObject();"にあるかもしれないと思います。 。しかし、私はそれを私はtoString()を追加しただけでなく、しかし、それは動作しませんでした修正する方法を知らないこれは重複しません(toString()は私の問題を解決しません!!!)オブジェクトを解析する際に問題があります。誰かが私を助けてくれますか?

package com.senac.leituradearquivos; 



public class Paciente { 

    private String nome; 
    private String rg; 
    private String dataNascimento; 

    public Paciente(String nome, String rg, String dataNascimento) { 
     super(); 
     this.nome = nome; 
     this.rg = rg; 
     this.dataNascimento = dataNascimento; 
    } 

    public Paciente() { 

    } 

    public String getNome() { 
     return nome; 
    } 

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

    public String getRg() { 
     return rg; 
    } 

    public void setRg(String rg) { 
     this.rg = rg; 
    } 

    public String getDataNascimento() { 
     return dataNascimento; 
    } 

    public void setDataNascimento(String dataNascimento) { 
     this.dataNascimento = dataNascimento; 
    } 

} 

package com.senac.leituradearquivos; 

import java.util.Scanner; 

public class PacienteParser implements CSVParser<Paciente> { 

    // dados = "Tammy Lawrence;402750779;1998-10-05" 
    // Paciente? 

    @SuppressWarnings("resource") 
    public Paciente parseObjects(String dados) { 

     Paciente paciente = new Paciente(); 
     Scanner leitor = new Scanner(dados); 
     leitor.useDelimiter("[;\n]"); 

     while (leitor.hasNext()) { 

      paciente.setNome(leitor.next()); 
      paciente.setRg(leitor.next()); 
      paciente.setDataNascimento(leitor.next()); 

     } 

     return paciente; 
    } 











    @SuppressWarnings("unused") 
    public static void main(String[] args) { 

     PacienteParser parser = new PacienteParser(); 

     String dados = "Tammy Lawrence;402750779;1998-10-05"; 

     Paciente p = parser.parseObjects(dados); // preenche os atributos de paciente 

     System.out.println(p.getNome()); 
     System.out.println(p.getRg()); 
     System.out.println(p.getDataNascimento()); 
    } 

} 


    package com.senac.leituradearquivos; 

public interface CSVParser<T> { 

    public T parseObjects(String dados); 

} 

    package com.senac.leituradearquivos; 

import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Scanner; 

public class CSVFile<T> { 

    private CSVParser<T> objectParser; 
    private FileReader fr; 

    @SuppressWarnings("resource") 
    public void open(String filename) { 

     try { 

      fr = new FileReader(filename); // abriu o arquivo 
      Scanner leitor = new Scanner(fr); // leu arquivo 
      leitor.useDelimiter("[;\n]"); 

     } catch (FileNotFoundException fnfe) { 

      System.err.println(fnfe.getMessage()); 
     } 

    } 

    public void close() throws IOException { 

     fr.close(); 

    } 

    public T readObject() { 

     // le uma linha do arquivo 
     // chamar o parse para converter os dados no objeto apropriado 

     // PacienteParser nParser = new PacienteParser(); 

     @SuppressWarnings("resource") 
     Scanner leitor = new Scanner(fr); // leu arquivo 
     leitor.useDelimiter("[\n]"); 

     return objectParser.parseObjects(leitor.next()); 

     // nParser.parseObjects(leitor.next()); 

     // return (T) objectParser; // return T precisa retornar um tipo 
     // genérico 
    } 

    public void setParser(CSVParser<T> parser) { 

     this.objectParser = parser; 

    } 

} 


package com.senac.leituradearquivos; 

import java.io.IOException; 

public class TestApplication { 

    public static void main(String[] args) throws IOException { 

     PacienteParser paciente = new PacienteParser(); 

     CSVFile<Paciente> file = new CSVFile<>(); 

     file.open("pacientes.csv"); 

     file.setParser(paciente); 
     System.out.println(file.readObject()); 

     file.close(); 




    } 

} 

例:。

Output

+0

「Paciente [nome =#fields:name、rg = RG 、dataNascimento = dataNascimento]」とtoString(); –

+0

あなたはこれを指定していません...あなたは何を得ると思いますか?あなたはあなたのCSVから1行だけを読みます。これが最初の行ですか?とにかく、あなたのメインのreadObjectをループしたいかもしれません... – Gilad

+0

それは私が助けを求めている理由です。私はtoString()を試してみましたが、readObject()でループし、メインでループしていました。メインでループするにはイテレータが必要です。メインのファイルをループすることはできません。 –

答えて

0

あなたはjaveしませんクラスPacienteのtoString()メソッド デフォルトのtoStringメソッドはクラス名とハッシュコードを出力します。オブジェクトフィールドを出力するオブジェクトを作成する必要があります。

+0

toString()で "Paciente [nome =#fields:name、rg = RG、dataNascimento = dataNascimento]"というメッセージが返されます。 CSVファイルの情報は表示されません。 –

関連する問題