file reading with scanner

Asked

Viewed 77 times

-1

I would love to know if anyone can help me with this code below, in my application the user will often 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 don’t know why.

If anyone can help me, I’d appreciate it.

    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 q got big

https://gist.github.com/fariasrod/d9d41e36856845327aa14110af5e3982

1 answer

0


The problem is that you try to access an index that doesn’t necessarily exist (name). You can do:

//separa os campos entre as virgulas de cada linha
String[] valoresEntreVirgulas = linhasDoArquivo.split(";");

ContatoDTO c = new ContatoDTO();
if(valoresEntreVirgulas.length > 0) {
    c.setTelefone(valoresEntreVirgulas[0]);
}
if(valoresEntreVirgulas.length > 1) {
    c.setNome(valoresEntreVirgulas[1]);
}
contato.add(c);

Or use regex:

public void adicionaArquivo(MultipartFile file) throws IOException {
    Pattern pattern = Pattern.compile("^([\\d]+)(?:;*)(.*)$");
    ....
    ....
    Matcher matcher = pattern.matcher(string);
    if (matcher.matches()) {
        ContatoDTO c = new ContatoDTO();
        if (matcher.groupCount() >= 1) {
          c.setTelefone(matcher.group(1));
        }

        if (matcher.groupCount() >= 2) {
          c.setNome(matcher.group(2));
        }
        contato.add(c);
    }

Browser other questions tagged

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