Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我无法让我的程序将数据从txt导入Jtable_Java_Arrays_File_Arraylist_Jtable - Fatal编程技术网

Java 我无法让我的程序将数据从txt导入Jtable

Java 我无法让我的程序将数据从txt导入Jtable,java,arrays,file,arraylist,jtable,Java,Arrays,File,Arraylist,Jtable,我的程序能够读取我的.txt文件,但由于某种原因,它无法将该数据放在我的表中。(如果您需要我的代码的任何其他部分,请告诉我)。我不确定到底是什么问题,但我几乎尝试了所有方法将其输入到表中,但似乎没有任何效果。我可以手动将数据输入到表中(使用“创建”按钮),但由于某些原因,我无法导入数据 //initializing all of the buttons (and jtable, scroll pane) this.jScrollPane1 = new JScrollPane(); this.po

我的程序能够读取我的.txt文件,但由于某种原因,它无法将该数据放在我的表中。(如果您需要我的代码的任何其他部分,请告诉我)。我不确定到底是什么问题,但我几乎尝试了所有方法将其输入到表中,但似乎没有任何效果。我可以手动将数据输入到表中(使用“创建”按钮),但由于某些原因,我无法导入数据

//initializing all of the buttons (and jtable, scroll pane)
this.jScrollPane1 = new JScrollPane();
this.pokeTable = new JTable();
this.programTitle = new JLabel();
this.searchText = new JTextField();
this.searchButton = new JButton();
this.createButton = new JButton();
this.deleteButton = new JButton();
this.displayInstructionsButton = new JButton();
this.exportPokedex = new JButton();

//sets close operation
setDefaultCloseOperation(3);

//makes my Jtable
  this.pokeTable.setModel(new DefaultTableModel(
  new Object[0][], 
  new String[] {"Name", "Region", "PokeNumber", "Type", "Habitat" }) //creating column headings
  {
    boolean[] canEdit = new boolean[5];

    public boolean isCellEditable(int rowIndex, int columnIndex) //makes all of the cells editable
    {
      return this.canEdit[columnIndex]; 
    }
  });


  IO.openInputFile("data/pokemon.txt");
  int j =0;
  while(IO.readLine()!=null){   
    j++;
  }
  IO.closeInputFile();

  IO.openInputFile("data/pokemon.txt");

  String[] pokemon2 = new String[5];

  for(int i = j; i!=0; i--)
  {

    String s = IO.readLine();
    pokemon2 = s.split(",");


    DefaultTableModel model = (DefaultTableModel)this.pokeTable.getModel();
    model.addRow(new Object[] { pokemon2[0], pokemon2[1], Integer.valueOf(pokemon2[2]), pokemon2[3], pokemon2[4] });
     new Pokemon (pokemon2[0], pokemon2[1], Integer.valueOf(pokemon2[2]), pokemon2[3], pokemon2[4]);
    for(int f = 0; f < pokemon2.length; f++){
        System.out.println(pokemon2[f]);
        System.out.println("");
    }
  }

  IO.closeInputFile();  

this.jScrollPane1.setViewportView(this.pokeTable); //makes table able to scroll
this.jScrollPane1.getViewport().setBackground(new Color (233, 116, 81)); //sets background of table (not cell colour)
//end of intializing JTable

//sets title of the program
this.programTitle.setText("Pokedex");
//初始化所有按钮(和jtable,滚动窗格)
this.jScrollPane1=新的JScrollPane();
this.pokeTable=新的JTable();
this.programTitle=new JLabel();
this.searchText=new JTextField();
this.searchButton=newjbutton();
this.createButton=new JButton();
this.deleteButton=newjbutton();
this.displayInstructionsButton=new JButton();
this.exportPokedex=new JButton();
//设置关闭操作
设置默认关闭操作(3);
//使我的Jtable
this.pokeTable.setModel(新的DefaultTableModel(
新对象[0][],
新字符串[]{“Name”、“Region”、“PokeNumber”、“Type”、“habitate”}//创建列标题
{
boolean[]canEdit=新的boolean[5];
public boolean isCellEditable(int rowIndex,int columnIndex)//使所有单元格都可编辑
{
返回此.canEdit[columnIndex];
}
});
openInputFile(“data/pokemon.txt”);
int j=0;
而(IO.readLine()!=null){
j++;
}
IO.closeInputFile();
openInputFile(“data/pokemon.txt”);
字符串[]pokemon2=新字符串[5];
对于(int i=j;i!=0;i--)
{
字符串s=IO.readLine();
口袋妖怪2=s.split(“,”);
DefaultTableModel=(DefaultTableModel)this.pokeTable.getModel();
model.addRow(新对象[]{pokemon2[0],pokemon2[1],Integer.valueOf(pokemon2[2]),pokemon2[3],pokemon2[4]});
新的口袋妖怪(pokemon2[0],pokemon2[1],Integer.valueOf(pokemon2[2]),pokemon2[3],pokemon2[4]);
对于(int f=0;f
我给你的建议是将不同的变量分开,你试图在一个步骤中做的太多,而不是

this.pokeTable.setModel(new DefaultTableModel(
new Object[0][],new String[] {"Name", "Region", "PokeNumber", "Type", "Habitat" })
创建单独的变量

String[] headers = {"Name", "Region", "PokeNumber", "Type", "Habitat" };
Object[][] arr = new Object[fileLength][headers.length];
DefaultTableModel model = new DefaultTableModel(arr, headers);
this.pokeTable.setModel(model);
一般来说,这可以消除许多小错误,然后继续编写检查语句以缩小问题发生的确切范围;当它工作时,您可以将它缩小为一行,但分解它通常足以发现并修复bug。在尝试创建表之前,可能先尝试填充数组。我拥有的一些代码实现了这一结果,但使用了数据库:

Object[][] data;

//Get data from table and convert to 2D array
data = new Object[len][columnNames.length];
String sql = "SELECT * FROM SHM";
ResultSet rs = st.executeQuery(sql);
int x = 0;
while (rs.next()){
    for(int i = 0; i < columnNames.length; i++) {
       data[x][i] = new Double(rs.getDouble(columnNames[i]));
    }                
    x++;
}
rs.close();
//Output data
JFrame frame = new JFrame("Table");
TableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.setAlwaysOnTop(true);
frame.pack();
frame.setVisible(true);
对象[][]数据;
//从表中获取数据并转换为二维数组
数据=新对象[len][columnNames.length];
String sql=“从SHM中选择*”;
结果集rs=st.executeQuery(sql);
int x=0;
while(rs.next()){
for(int i=0;i

尝试以这种格式实现它,将其拆分以确保您的工作正常,在将其放入表之前从文件创建2Darray,而不是创建表并尝试以这种方式向其添加新行;如果以后要添加新行,请让它重新绘制表格。如果这仍然不起作用,我不确定是否抱歉。

使用SO作为调试器是一个糟糕的主意。就调试小程序而言,您尝试了什么?您是否尝试调试或打印值以测试断言?