Stargazer
Stargazer

Reputation: 1530

For loop skipping assignments

I have a program that reads a txt file and show the information in a table. I was doing pretty well, but my program is not assign the value to the matrix position when the for loop reach the positions 5 and 7. But the odd is that the other positions work just fine.

Here is my code

public class Main extends JPanel {
private boolean DEBUG = false;

public Main() throws IOException {
    super(new GridLayout(1,0));

    String[] columnNames = {"Nome",
                            "CPF",
                            "Logradouro",
                            "Complemento",
                            "CEP",
                            "Cidade",
                            "Estado",
                            "Telefone 1",
                            "Telefone 2"};

    Object[][] data = { { "","","","","","","","",""}   

    };

    String segueLista = null;
    //BufferedReader leDados = new BufferedReader(new FileReader("C:\\Teste\\lista.txt"));
   // File lista1 = new File("C:\\Teste\\lista.txt");
    ArrayList<String> lista = new ArrayList<String>();
    //lista = (ArrayList<String>) Files.readAllLines(lista1.toPath());
    int i = 0;
    int i2 = 0;
    BufferedReader leDados = new BufferedReader(new FileReader("C:\\Teste\\072.612.516-40.txt"));


    for(i = 0; i < 9; i++) {
        if((segueLista = leDados.readLine()) != null) {
            if(!segueLista.equals("")){
                JOptionPane.showMessageDialog(null, segueLista);
                data[0][i] = segueLista;
                i--;
            }
        }

    }



    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(900, 70));
    table.setFillsViewportHeight(true);

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

private void printDebugData(JTable table) {
    int numRows = table.getRowCount();
    int numCols = table.getColumnCount();
    javax.swing.table.TableModel model = table.getModel();

    System.out.println("Value of data: ");
    for (int i=0; i < numRows; i++) {
        System.out.print("    row " + i + ":");
        for (int j=0; j < numCols; j++) {
            System.out.print("  " + model.getValueAt(i, j));
        }
        System.out.println();
    }
    System.out.println("--------------------------");
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 * @throws IOException 
 */
private static void createAndShowGUI() throws IOException {
    //Create and set up the window.
    JFrame frame = new JFrame("SimpleTableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    Main newContentPane = new Main();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                createAndShowGUI();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

}

And this is the info that it reads from the txt file

Upvotes: 0

Views: 52

Answers (1)

Eran
Eran

Reputation: 393841

It seems your for loop overwrites the elements in the data array, since whenever you assign a value to data[0][i], you also decrement i, which means that data may be overwritten in the next iteration.

Perhaps you meant to decrement i only when the input line is empty :

for(i = 0; i < 9; i++) {
    if((segueLista = leDados.readLine()) != null) {
        if(!segueLista.equals("")){
            JOptionPane.showMessageDialog(null, segueLista);
            data[0][i] = segueLista;
        } else {
            i--;
        }
    }

}

Upvotes: 1

Related Questions