Ignore the header when inserting data from a csv file into a Java string array

Asked

Viewed 150 times

0

Good night,

I read a csv file and stored the information in a string array. The file from position 0 to 8 has string content, from position 9 to 29 has in its entire content. I need to compare if the content of position 9 to 29 is different from 0 , for this I needed to convert my string vector into an integer vector of position 9 to 29, however, it is giving the error precisely with the description of column 9:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Coluna9"
at java.lang.NumberFormatException.forInputString(Unknown Source)

So in order to resolve this error, I think I need to ignore line 1. How do I start filling the vector from the second line? Below code that reads the csv and makes the conversion to the integer vector:

private static final String VIRGULA = ",";

public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader
            (new InputStreamReader(new FileInputStream("C:/Users/Desktop/workspace/geral/src/teste.csv")));
    String linha = null;

    while ((linha = reader.readLine()) != null) {
        String[] dados = linha.split(VIRGULA);
        System.out.println(Arrays.toString(dados));


         int[] valorInteiros = new int[dados.length];

        for (int i = 9; i < dados.length; i++) {
            valorInteiros[i] = Integer.parseInt(String.valueOf(dados[i]));
            System.out.println(valorInteiros[i]);
        }

        System.out.println("--------------------------");
    }
    reader.close();


}

1 answer

0


ADR, have you thought about putting the first line reading before the while? I confess that it is not very elegant, but I believe it solves your problem without generating problems in your logic:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

class Main {
  private static final String VIRGULA = ",";

  public static int converteInteiro(String valor) {
    try {
      return Integer.parseInt(valor);
    } catch (NumberFormatException e) {
      return 0;
    }
  }

  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("teste.csv")));
    String linha = null;

    if (reader.readLine() != null) {
      while ((linha = reader.readLine()) != null) {
          String[] dados = linha.split(VIRGULA);
          System.out.println(Arrays.toString(dados));

          int[] valorInteiros = new int[dados.length];

          for (int i = 9; i < dados.length; i++) {
              valorInteiros[i] = converteInteiro(String.valueOf(dados[i]));
              System.out.println(valorInteiros[i]);
          }

          System.out.println("--------------------------");
      }
    }
    reader.close();
  }
}

See working here: https://repl.it/repls/RedInstructiveCrash

  • It worked, however, at the end of the run it gave the following error: Exception in thread "main" java.lang.Numberformatexception: For input string: "" at java.lang.Numberformatexception.forInputString(Unknown Source) at java.lang.Integer.parseint(Unknown Source) at java.lang.Integer.parseint(Unknown Source) at general.Main.main(Main.java:26) line 26 refers to: valueInters[i] = Integer.parseint(String.valueOf(data[i]); Why will it be?

  • You need to check your CSV, maybe it has some value that is not agreed, put it there for us to analyze.

  • True, it’s csv, it’s got some columns that aren’t filled with integer, it’s got nothing, so it can’t convert empty "" to an integer. Is there any way I can put a treatment not to give that type Convert Xception " 0?

  • Yes, I changed the code with an example, the convert returns 0 in the conversion exception.

  • Thanks, it worked out.

Browser other questions tagged

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