I would love to know if anyone can help me with this code below, in my application the user will frequently upload some files and they have two information, number and name separated by -> ; but in the file I simulate some possible problems like unnamed numbers and my code does not behave well against these possible problems.
Another detail is that my SOUT is duplicating and I do not know why.
If someone can help me, thank you.
public void adicionaArquivo(MultipartFile file) throws IOException {
InputStream inputStream = file.getInputStream();
List<ContatoDTO> contato = new ArrayList<>();
//cria um scanner para ler o arquivo
Scanner leitor = new Scanner(inputStream);
//variavel que armazenara as linhas do arquivo
String linhasDoArquivo = new String();
//percorre todo o arquivo
while (leitor.hasNext()) {
try {
//recebe cada linha do arquivo
linhasDoArquivo = leitor.nextLine();
//separa os campos entre as virgulas de cada linha
String[] valoresEntreVirgulas = linhasDoArquivo.split(";");
for (int i = 0; i < valoresEntreVirgulas.length; i++) {
ContatoDTO c = new ContatoDTO();
c.setTelefone(valoresEntreVirgulas[0]);
c.setNome(valoresEntreVirgulas[1]);
contato.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 0; i < contato.size(); i++) {
System.out.println("telefone : " + contato.get(i).getTelefone());
System.out.println("Nome : " + contato.get(i).getNome());
}
System.out.println("qt de linhas : " + contato.size());
}
My file
71999998811;nome1
71999998812;nome2
71999998813;nome3
71999998814;nome4
71999998815;nome5
71999998816;nome6
71999998817;nome7
71999998818;nome8
71999998819;nome9
71999998810;nome10
71999998810;nome11
71999998810
71999998810;
71999998810;nome14
71999998855;nome15
My error log has gotten big