2015-03-30 151 views
0

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

这里是我的代码

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文件

蒂亚戈坎迪博尔赫斯

072.612.516-40读

Av Niza Marq的UE瓜里塔1315

Bloco 35 APTO 204

38082-000

乌贝拉巴

MG

(34)3315-8807(34)9908-7502

回答

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

} 
+0

OMG!完善!非常感谢。 – Blackk 2015-03-30 21:25:01