0

I'm using a dat file to store underscore separated values and comma separated values at the same time but every time I want to get the values of a the second String[] called basics in order to set values of another object but it suddenly resets it's size and stores values to the first splitted one.

String data in this case would be like:

String data = "0234_ADMIN_12-Jun-2022 21:58:59,5635_PAL_16.0_54";

private void cargarNotas() {
    try {
        BufferedReader leerNotas = new BufferedReader(new FileReader(regNotas));
        String data = leerNotas.readLine();
        while (data!= null) {
            String[] items = data.split(",");
            String[] basics = items[0].trim().split("_");
            System.out.println(basics.length);
            NotaDeVenta tempNota = new NotaDeVenta();
            tempNota.setnoNota(basics[0]);
            tempNota.setUsuario(basics[1]);
            tempNota.setFecha(FORMAT.parse(basics[2]));
            String[] values = null;
            for(int i = 0;i<items.length-1;i++) {
                values = items[i+1].split("_");
                ItemParaMovimientos tempItem = new ItemParaMovimientos(Control.getProducto(values[1]),Integer.parseInt(values[3]));
                tempNota.addItem(tempItem);
            }
            Control.addNota(tempNota);
            data = leerNotas.readLine();
        }
        leerNotas.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

length Output

Chetan Ahirrao
  • 1,454
  • 11
  • 16
  • 1
    Please don't post images of text. – tgdavies Jun 13 '22 at 04:00
  • 1
    It isn't clear what you're asking, but you do know that each time around your while loop you set `basics` to a new value? – tgdavies Jun 13 '22 at 04:01
  • 1
    What happens if `items` or `basics` have a length of 1 (index 0) but you attempt to access a value higher than 0? Correct, you get an index out of bounds exception. Think about these lines, and what happens when `items` and `basics` have a length that is not what you expect, for example `basics[1]` or `basics[2]` when the array may only have a length of 1 `tempNota.setFecha(FORMAT.parse(basics[2]));`, and `i+1` here `values = items[i+1].split("_");` when the length of items will only be 1 (or index 0) with the example input string – sorifiend Jun 13 '22 at 04:08
  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – sorifiend Jun 13 '22 at 04:08
  • I second what @sorifiend said above. You'll need to inspect your data and handle scenarios where your array size = 1. For example, in your output, your System.out.println() statement is first resulting in 3 as basics.length when things are working fine and then 1 as basics.length when you get an ArrayIndexOutOfBoundsException because there is no index 1 or 2 to read from. – legendofawesomeness Jun 13 '22 at 04:31

0 Answers0