Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/06/2011, 00:35
Spassky
 
Fecha de Ingreso: junio-2011
Mensajes: 7
Antigüedad: 12 años, 10 meses
Puntos: 0
Problema con Integer.parseInt()

Hola amigos,

Se me presento un problema que me tiene un poco contrariado ya que no se porque me salta este error, resulta que estoy leyendo de un archivo txt que tiene 2 campos, un nombre y un numero...

Como el dato nombre ya esta como String lo dejo ahi pero el numero quiero parsearlo a un int para poder meterlo a una base de datos...pero al tratar de parsearlo me arroja un error...si dejo la cadena con numeros sin parsear me lee el numero como String sin ningun problema, agradeceria mucho alguien que me pudiera ayudar soy bastante nuevo en esto...

Codigo

Código:
    

public static void main(String[] args) {

        File archivo = null;
        FileReader fr = null;
        BufferedReader br = null;
        String linea = null;

        try {
            archivo = new File("\\\\192.168.1.100\\g\\prueba.txt");
            fr = new FileReader(archivo);
            br = new BufferedReader(fr);
            String[] datos = null;
            String numero = null;
            int num = 0;
            while ((linea = br.readLine()) != null) {
                datos = linea.split(";");
                String nombre = datos[0];
                numero = datos[1]; 
                num = Integer.parseInt(numero); 
                int numero = Integer.parseInt(num);
                System.out.println(nombre);
                System.out.println(num);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
Error

Código:
java.lang.NumberFormatException: For input string: " 4353"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:449)
        at java.lang.Integer.parseInt(Integer.java:499)
        at lectura.main(lectura.java:45)