Why do I get java.lang.NumberFormatException: For input string: "1"

0

Good evening, I'm reading a txt and I want to insert the data it has to a mysql table but I skip this exception:

  

NumberFormatException: For input string: "1".

 File archivo = new File ("D:\"+nombretxt+".txt");
Scanner s = new Scanner (archivo);
PreparedStatement pst=conexion.prepareStatement("INSERT INTO 'countries'  (id, country_code, country_name) VALUES (?,?,?)");
while (s.hasNextLine()){
String linea =s.nextLine();
Scanner s2 = new Scanner (linea);
s2.useDelimiter("[;\n]");



   pst.setInt(1,Integer.parseInt(s2.next())); //Aca me larga el error
   pst.setString(2,s2.next());
   pst.setString(3, s2.next());
pst.executeUpdate();




}
s.close();
    
asked by facuk 26.09.2017 в 02:28
source

2 answers

0

It is better to control the code, and not that the code controls you, forcing you to make type conversions later.

If there is a whole value in each line, you can search it in the Scanner:

while (s.hasNextLine())
{
    String linea =s.nextLine();
    Scanner s2 = new Scanner (linea);
    s2.useDelimiter("[;\n]");

    if (s2.hasNextInt()) 
    {
        int intValor=s2.nextInt();
    }

    //Para mi gusto almacenaría todo en variables
    pst.setInt(1,intValor); //Aca me larga el error
    pst.setString(2,s2.next());
    pst.setString(3, s2.next());
    pst.executeUpdate();

}
s.close();
    
answered by 26.09.2017 / 12:04
source
0

Remove the enter:

Scanner s2 = new Scanner (linea.trim());

Updated:

If you are trying to enter an integer then use:

int linea =s.nextInt();
    
answered by 26.09.2017 в 02:30