Error String to Int Conversion

Asked

Viewed 376 times

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();

}  
  • 1

    And what’s the problem?

  • 1

    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.

  • You’ve debugged to see what’s in your String[] valores ?

  • Post the Logger with the stacktrace of the exception to see what is the error that occurs

  • Make sure that the first line of your CSV contains the file header, you are probably trying to convert this header.

  • 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.

  • @Marcoantonio, please post the solution used as a response!

Show 2 more comments

1 answer

2

This code assumes that the first field of each CSV line is an integer:

        String[] valores = linhaDoArquivo.split(",");
        int instante = Integer.parseInt(valores[0]);

However it is common to have headers in the CSV with the title of each column, which would cause a Numberformatexception:

java.lang.Numberformatexception: For input string: "ID" at
java.lang.Numberformatexception.forInputString(Numberformatexception.java:65)

So the solution is to adjust the CSV or, even better, protect the code:

String[] valores = linhaDoArquivo.split(",");

try {
      int instante = Integer.parseInt(valores[0]);
} catch (NumberFormatException e){
    //algo deu errado. Tratar aqui
    System.err.println(valores[0] + " não é numérico!");
}

Browser other questions tagged

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