Read a txt file and pass it to a list

0

txt that I want to put in a list:

  

0.1.0.08,0.04.0.01.0.04

     

0.2.0.15.0.02.0.0.06

txt meaning:

  

QuantityNutrients1, quantityNutrients2, quantityNutrients3, quantityNutrients4, cost

each line being an ingredient

public void leeArchivo(String file) throws FileNotFoundException, IOException {
    String cadena;
    FileReader f = new FileReader(file);
    BufferedReader b = new BufferedReader(f);

    while((cadena = b.readLine())!=null) {
        String[] parts = cadena.split(",");
        Ingrediente ing = new Ingrediente();
        ing.setCantidadNutrientes(Double.parseDouble(parts[0]));
        ing.setCoste(Double.parseDouble(parts[1]));
    }

    b.close();
}

The idea is to put the txt in a List<Ingrediente> , to be able to work on that list. Although now that I think about it, since I have several ingredients, it would be more of a List<List<Ingrediente>> or so I think.

I have a class Ingrediente like:

public class Ingrediente {

    private Integer codigo;
    private Double cantidadNutrientes;
    private Double coste;

    public Ingrediente(Double cantidadNutrientes, Double coste) {
        super();
        this.codigo = null;
        this.cantidadNutrientes = cantidadNutrientes;
        this.coste = coste;
    }

    public Ingrediente(){
        this.cantidadNutrientes = new Double(cantidadNutrientes);
        this.coste = new Double(coste);
    }

    public Ingrediente(String s) {
        String[] v = s.split("[ ,]");
        Integer ne = v.length;
        if (ne != 1)
            throw new IllegalArgumentException("Formato no adecuado en línea  " + s);
        cantidadNutrientes = new Double(v[0]);
        coste = new Double(v[1]);
    }

    public Ingrediente(Integer codigo, String[] s) {
        super();
        this.codigo = codigo;
        this.cantidadNutrientes = new Double(s[0]);
        this.coste = new Double(s[1]);

    }

    public static Ingrediente create(String s) {
        return new Ingrediente(s);
    }

    public static Ingrediente create(Integer codigo, String[] s) {
        return new Ingrediente(codigo, s);
    }

    public Integer getCodigo() {
        return codigo;
    }

    public void setCodigo(Integer codigo) {
        this.codigo = codigo;
    }

    public Double getCantidadNutrientes() {
        return cantidadNutrientes;
    }

    public void setCantidadNutrientes(Double cantidadNutrientes) {
        this.cantidadNutrientes = cantidadNutrientes;
    }

    public Double getCoste() {
        return coste;
    }

    public void setCoste(Double coste) {
        this.coste = coste;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((cantidadNutrientes == null) ? 0 : cantidadNutrientes.hashCode());
        result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
        result = prime * result + ((coste == null) ? 0 : coste.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Ingrediente other = (Ingrediente) obj;
        if (cantidadNutrientes == null) {
            if (other.cantidadNutrientes != null)
                return false;
        } else if (!cantidadNutrientes.equals(other.cantidadNutrientes))
            return false;
        if (codigo == null) {
            if (other.codigo != null)
                return false;
        } else if (!codigo.equals(other.codigo))
            return false;
        if (coste == null) {
            if (other.coste != null)
                return false;
        } else if (!coste.equals(other.coste))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "" + cantidadNutrientes + coste + "";
    }

} 
    
asked by fblanco89 06.04.2018 в 20:26
source

1 answer

0

I think your problem is here:

 while((cadena = b.readLine())!=null) {
            System.out.println(cadena);
    }

    b.close();
    return cadena;

The while loop runs until the string = null, and then you return the string. Your function will always return null;

    
answered by 06.04.2018 в 20:52