2
I’m doing a simple program, where it reads a file . csv (Excel) and then generates a graph of it. However I am having problems when converting String to Int. Below is the code of the Button event:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
FileDialog abrir = new FileDialog(new Dialog(this),"Abrir arquivo",FileDialog.LOAD);
abrir.setVisible(true);
File arquivo = new File(abrir.getDirectory()+abrir.getFile());
XYSeries temp = new XYSeries("Temperatura");
try {
String linhaDoArquivo;
Scanner lerArquivo = new Scanner(arquivo);
while(lerArquivo.hasNext()){
linhaDoArquivo = lerArquivo.next();
String[] valores = linhaDoArquivo.split(",");
int instante = Integer.parseInt(valores[0]);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(temp);
JFreeChart chart = ChartFactory.createXYLineChart("Temperatura", "Instante", "Temperatura", dataset, PlotOrientation.VERTICAL, true, true, false);
ChartPanel panel = new ChartPanel(chart);
jPanel1.removeAll();
jPanel1.add(panel, BorderLayout.CENTER);
jPanel1.validate();
}
And what’s the problem?
– Maniero
Besides describing the problem, it would be interesting if you put the first 3 lines of your CSV. By the question title, you must have something in the first position of any of the lines that generates an exception when converting to integer.
– cantoni
You’ve debugged to see what’s in your
String[] valores
?– Marco Souza
Post the Logger with the stacktrace of the exception to see what is the error that occurs
– Julio Borges
Make sure that the first line of your CSV contains the file header, you are probably trying to convert this header.
– Hoppy
Sorry it took me so long to reply, but the error was in the header of csv itself, I tidied up and worked perfectly. Thank you.
– Marco Antonio
@Marcoantonio, please post the solution used as a response!
– Genos