0
I’m trying to read a txt file, select a specific column called "customerid", take all the values from that column and show on the screen how many different values there are.
But when I try to do that, HashSet
always returns me value null
. I can’t understand what my mistake is or where I’m not loading the data correctly. Can anyone help me?
Code:
package com.nayana.exercicio1.methods;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
public class Methods {
private BufferedReader file;
private HashSet<String> customer;
private int lines;
private String[] columns;
public String line;
//INPUT PARA O ARQUIVOO A SER LIDO
public BufferedReader getFile() {
return file;
}
public void setFile(BufferedReader file) throws FileNotFoundException {
this.file = file;
}
//CONTAR NÚMERO DE LINHAS EXISTENTES
public int getLines() {
return lines;
}
public String getLine() {
return line;
}
public void setLines(int lines) throws IOException {
this.lines = lines;
}
public String[] getColumns() {
return columns;
}
public void setColumns(String[] columns) {
this.columns = columns;
columns = ((String)line).split(";");
}
public void setLine(String line) throws IOException {
this.line = file.readLine();
}
public void contarLinhas() throws Exception {
while((line = file.readLine()) !=null) {
setColumns(columns);
lines++;
}}
//CONTAR QUANTOS COSTUMERID DIFERENTES EXISTEM
public HashSet<String> getCustomer() {
return customer;
}
public void setCustomer(HashSet<String> customer) throws IOException {
this.customer = new HashSet<String>();
}
public void customerId() throws Exception {
setFile(file);
while((line = file.readLine()) !=null) {
String [] columns = line.split(";");
customer.add(columns[4]);
customer.size();
setCustomer(customer);
}
}
//SALTAR LINHA
public void saltarLinha() throws IOException {
line = file.readLine();
}
//MOSTRAR NA TELA
public void status(){
System.out.println("\nO número total de linhas é: " + getLines());
System.out.println("\nO número total de CustomerId é: " + getCustomer());
}
Main:
package com.nayana.exercicio1;
import java.io.BufferedReader;
import java.io.FileReader;
import com.nayana.exercicio1.methods.Methods;
public class Exercicio1 {
public static void main(String[] args) throws Exception {
Methods exercicio = new Methods();
exercicio.setFile(new BufferedReader(new FileReader("C:\\Users\\nayan\\Downloads\\orders_04_20_07.txt")));
exercicio.contarLinhas();
exercicio.saltarLinha();
exercicio.customerId();
exercicio.status();
}
}
Man, thank you so much. I was banging my head for a long time to understand, but really when we are beginners we complicate too much something that is even very simple. I am very grateful for the availability and for all the explanation!
– Nayana Ciuro