I have a text file (test.txt) :
Bob, 12, 15, 20
Ruth, 45, 212, 452
With Java, I want to extract only the last element of each line (each element being separated by a coma).
For now on, I wrote this code :
br = new BufferedReader(new FileReader("test.txt"));
while ((line = br.readLine()) != null) {
String[] facture = line.split(",");
int fquantite = Integer.parseInt(facture[3]);
System.out.println("Amount=" + fquantite);
But it gives me an error. The thing is that I get how to get the number (for exemple, I can write :
System.out.println("Amount=" + facture[3]);
And it works, but for some reason, I can't get to convert it to a int. The reason I want to do that is because when I'll have this int variable, I'll want to add it to another int variable.
You split by comma, but your input also contains spaces. Use trim
to remove them: Integer.parseInt(facture[3].trim())
.