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();
}
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?
– ADR
You need to check your CSV, maybe it has some value that is not agreed, put it there for us to analyze.
– Daniel Mendes
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?
– ADR
Yes, I changed the code with an example, the convert returns 0 in the conversion exception.
– Daniel Mendes
Thanks, it worked out.
– ADR