import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class DataDisplay extends JFrame{
JTable table;
public static JLabel jl1= new JLabel("Activity ID");
public static JTextField jt1= new JTextField("Enter Activity ID");
public static JLabel jl2= new JLabel("Activity name");
public static JTextField jt2 = new JTextField("Enter Activity name");
public static JLabel jl3= new JLabel("Start Date");
public static JTextField jt3= new JTextField("Enter Start Date");
public static JLabel jl4 = new JLabel("End Date");
public static JTextField jt4= new JTextField("Enter End Date");
public static JLabel jl5 = new JLabel("Alarm Before");
public static JTextField jt5= new JTextField("enter alarm");
public static JLabel jl= new JLabel("HOURS");
public static JButton save = new JButton("SAVE");
public static JButton close = new JButton("CLOSE");
public Container c= this.getContentPane();
private static int counter=0;
DataDisplay(){
this.setTitle("Assignment 3");
this.setLayout(null);
this.setBackground(Color.MAGENTA);
c.add(jl1);
c.add(jt1);
c.add(jl2);
c.add(jt2);
c.add(jl3);
c.add(jt3);
c.add(jl4);
c.add(jt4);
c.add(jl5);
c.add(jt5);
c.add(jl);
jl1.setBounds(10, 10, 100, 100);
jt1.setBounds(100, 45, 100, 30);
jl2.setBounds(10,60,100,100);
jt2.setBounds(100,100,120,30);
jl3.setBounds(10,110,100,100);
jt3.setBounds(100,145,120,30);
jl4.setBounds(10,160,100,100);
jt4.setBounds(100,200,120,30);
jl5.setBounds(10,210,100,100);
jt5.setBounds(100,250,120,30);
jl.setBounds(230,210,100,100);
c.add(save);
save.setBounds(10,300,100,30);
save.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
int ce=0;
// TODO Auto-generated method stub
String tableHeader[] ={"Activity ID", "Activity name", "Start Date", "End Date","Alarm before (Hours)"};
String tableData[][]= new String[ce=counter+1][10];
table = new JTable(tableData, tableHeader);
String line=null;
BufferedReader b=null;
try {
b = new BufferedReader(new FileReader("Activity.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while ((line=b.readLine())!=null){
String[] s=null;
int row=1;
for(int counterc=1; counterc<=5; counterc++ ){//*ARRAY OUT OF BOUND EXCEPTION*
s=line.split("|",4);
}
***for(int loop=0;loop<=5;loop++){
//here gives exception
tableData[row++][loop]=s[loop];***
}
row++;
counter++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tableData[counter][0]=jt1.getText();
tableData[counter][1]=jt2.getText();
tableData[counter][2]=jt3.getText();
tableData[counter][3]=jt4.getText();
tableData[counter][4]=jt5.getText();
JScrollPane scrollPane = new JScrollPane(table);
c.add(scrollPane, BorderLayout.CENTER);
scrollPane.setBounds(10, 400, 700, 600);
counter++;
try{
FileWriter out= new FileWriter("Activity.txt",true);
BufferedWriter br= new BufferedWriter(out);
PrintWriter p= new PrintWriter(br);
p.println(jt1.getText()+"|"+jt2.getText()+"|"+jt3.getText()+"|"+jt4.getText()+"|"+jt5.getText());
p.close();
}
catch(IOException e){
e.getStackTrace();
}
}
});
c.add(close);
close.setBounds(200,300,100,30);
this.setBounds(100,100,1000,1000);
this.setVisible(true);
c.setVisible(true);
}
public static void main(String args[]){
DataDisplay dd= new DataDisplay();
}
}
上面的程序显示表格,并在点击“保存”按钮时保存到屏幕上的文件中。当用户再次执行程序时,最后一个数据不会丢失,但会显示在屏幕上。文件中的数据由&#34; |&#34;分隔。运营商。所以我试图做的是循环读取行直到文件结束并在管道操作符的基础上拆分它。然后for循环在表格上显示数据。但是,我的程序正在给出ARRAY OUT OF BOUND EXCEPTION,我无法解决它。所以请帮忙!
答案 0 :(得分:2)
您的s
数组不会超过4个元素。来自docs:
limit参数控制模式的应用次数,因此会影响结果数组的长度。如果限制 n 大于零,则模式将最多应用 n - 1次,数组的长度不会大于 n ,数组的最后一个条目将包含除最后一个匹配分隔符之外的所有输入...
您使用s
设置s=line.split("|",4);
。您的 n 此处为4,表示s
的长度不超过4个。
当你像这样迭代s
时:
for(int loop=0;loop<=5;loop++){
tableData[row++][loop]=s[loop];
}
您正在尝试访问4元素数组的元素5。将loop <= 5
切换为loop < 5
。