用于循环跳过分配

时间:2015-03-30 19:50:34

标签: java swing object matrix

我有一个程序可以读取txt文件并在表格中显示信息。我做得很好,但是当for循环到达位置5和7时,我的程序没有将值赋给矩阵位置。但奇怪的是其他位置工作正常。

这是我的代码

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();
            }
        }
    });
}

}

这是从txt文件中读取的信息

1 个答案:

答案 0 :(得分:1)

似乎你的for循环覆盖了数据数组中的元素,因为每当你为data[0][i]赋值时,你也会递减i,这意味着在下一次迭代中可能会覆盖数据。

也许你只想在输入行为空时递减i

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

}