Error loading the file

1

I'm working with files, but I get two errors

  

For input string: "Eduardo" java.lang.NumberFormatException: For input   string: "Eduardo"

before that I was throwing a NoSuchElementException error but I already solved it

This is my code to upload the file

public void cargar_txt(){
    File ruta = new File(ruta_txt);
    String linea = null;
    int ln = 0;
    try{
        FileReader fr = new FileReader(ruta);
        BufferedReader br = new BufferedReader(fr);
        dc = null;
        while((linea = br.readLine()) != null){
            ln++;
            try{
                StringTokenizer st = new StringTokenizer(linea, ", ");
                while(st.hasMoreElements() && st.nextToken() != null){
                    dc = new DatosCliente();
                    dc.setCuenta(Integer.parseInt(st.nextToken()));
                    dc.setNombre(st.nextToken());
                    dc.setPaterno(st.nextToken());
                    dc.setMaterno(st.nextToken());
                    dc.setSaldo(Double.parseDouble(st.nextToken()));
                    dc.setPassword(st.nextToken());
                    pb.AgregarCliente(dc);
                }
            }catch(NoSuchElementException ex){
                System.out.println(String.format("NSE in %d: %s", ln, linea));
            }
        }
        br.close();
    }
    catch(Exception e){
        mensaje("Error al cargar el archivo: " + e.getMessage());
        mensaje("Error: " + e);
        System.out.println(e.getMessage());
        System.out.println(e);
    }
}
    
asked by Luis9724 03.10.2017 в 03:22
source

1 answer

0

Actually the problem is not loading the file, the problem is that when you get the first token you get when separating by "," it's not a numeric type ,

 dc.setCuenta(Integer.parseInt(st.nextToken()));

when trying to convert it to integer value, when it is not numeric, it causes the error:

  

For input string: .... java.lang.NumberFormatException: For input string: ....

in fact the value of the token is "Eduardo".

I recommend you avoid reading the first token in this line using nextToken() and only use hasMoreElements() to determine if an available token is found:

while(st.hasMoreElements() /* && st.nextToken() != null*/){
...
...

this may be the cause of the problem if the first token is the one you want to convert to an entire.

    
answered by 03.10.2017 в 05:19