我想创建一个文件验证器,因此,导入一个csv文件,检查内容,检查每个字段不是空白,然后检查每个字段的长度。如果它大于指定的值(对于每个字段不同),则我想将其显示为不同的颜色,以便我可以轻松识别任何不适合我的数据库的字段。如何使用数组来验证另一个数组中的元素长度?
我设法将文件读入数组,检查它是否为空,然后根据静态值检查字段长度。我有另一个数组包含字段大小,但我似乎无法得到交叉引用dataArrayElement 0与valaidateArrayElement 0.
我已经得到它输出到JTable,并显示任何命中“TOOBIG”在不同的颜色。
Integer[] validateLength = {8,2,22,11,25,13,6,2,34,11,35,35,34,11,1};
class checkValue{
public String validate(String value){
// Check to see if the cell is null or blank
if (StringUtils.isNullOrBlank(value)) {
return "EMPTY";
} else if (value.length() > 8) { //Work out how to set this as differing field lengths
return "TOOBIG";
}
else {
return "OK";
}
}
}
class CustomRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 6703872492730589499L;
private checkValue cv = new checkValue();
public JComponent getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JComponent cellComponent = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (cv.validate(table.getValueAt(row, column).toString()).equals("TOOBIG")){
cellComponent.setBackground(Color.RED);
cellComponent.setForeground(Color.YELLOW);
cellComponent.setToolTipText("Data is too big for the DB");
} else if(cv.validate(table.getValueAt(row, column).toString()).equals("EMPTY")){
cellComponent.setBackground(Color.GRAY);
cellComponent.setForeground(Color.WHITE);
cellComponent.setToolTipText("Field is empty in import file!");
} else {
cellComponent.setBackground(Color.WHITE);
cellComponent.setForeground(Color.BLACK);
}
return cellComponent;
}
}
任何帮助,将不胜感激!
感谢
*“我怎样才能使用数组来验证另一个数组中的元素长度?“*和.. ***你认为这与** Swing **有关吗? –
我在JFrame上显示它,当我创建这个问题时,它建议我添加Swing标签,如果这增加了混淆,请致歉。 – Dan
将该字段的索引(我猜它是'column')传递给'validate(String value)'并使用该索引来访问'validateLength'(有一些设计问题,但我现在会忽略它们) 。 – Thomas