java.lang.NumberFormatException error: For input string: "andres; rodrigues; [email protected]; 18" [closed]

-7

I'm trying to read a txt file on a jframe and I get the following error:

  

ejava.lang.NumberFormatException: For input string: "andres; rodrigues; [email protected]; 18"

the frame is four columns

private Object[][] leerusuarios()   {
    Object[][] data;
    try {
        BufferedReader br = new BufferedReader(new FileReader("usuarios.txt"));
        String linea= br.readLine();
        int numFilas=Integer.parseInt(linea);
        data= new Object [numFilas][4];
        for(int i=0;i<numFilas;i++){
            linea=br.readLine();
            String[] datosUsuario=linea.split(";");
            for(int j=0;j<4;j++){
                data [i][j]=datosUsuario[j];
                System.out.println(br.readLine());

                br.close();

...

    } catch (IOException ex) {

        System.out.println(ex.getMessage());
        data= new Object[0][4];
    }
    return data;
}

Archivo usuarios.txt:

andres;rodrigues;[email protected];18 juan;hernandes;[email protected];20 maria;castro;[email protected];30 david;peña;[email protected];26
    
asked by oscar riveros 22.12.2016 в 02:43
source

2 answers

1

The corrected code is. It was bad how it was implemented.

private Object[][] leerusuarios()   {
    Object[][] data;
    try {
        BufferedReader br = new BufferedReader(new FileReader("usuarios.txt"));
        String linea = br.readLine();
        String[] filas = linea.split(" ");
        int numFilas = filas.length;
        data = new Object [numFilas][4];
        for(int i = 0; i < numFilas; i++){
            linea = br.readLine();
            String[] datosUsuario = filas[i].split(";");
            for(int j = 0; j < datosUsuario.length; j++){
                data[i][j] = datosUsuario[j];
                System.out.println(data[i][j]);
            }
        }
        // Se cierra porque ya no se usa mas.
        br.close();
    }catch (IOException ex) { 
         System.out.println(ex.getMessage()); 
         data = new Object[0][4]; 
    } 
    return data;
}
    
answered by 22.12.2016 / 03:22
source
0

You have not explained the details of how the program is supposed to work with the file. But according to the following portion:

BufferedReader br = new  BufferedReader(new FileReader("usuarios.txt"));
String linea= br.readLine();
int numFilas=Integer.parseInt(linea);

... it seems that the idea is that the first line of your file should contain a numeric value that represents the number of rows and then you use to determine how many more lines you should read and process from the file.

But in your case, the first line contains the value:

  

andres; rodrigues; [email protected]; 18

... that obviously can not be converted to an integer, hence your error.

It seems to me that all you need is to add an initial line to your file with the number of rows.

Example:

  

4
  andres; rodrigues; [email protected]; 18
  juan; hernandes; [email protected]; 20
  Maria, castro, [email protected]; 30
  david; peña; [email protected]; 26

    
answered by 22.12.2016 в 03:32