[英]What to do if i keep getting error: Local variable j defined in an enclosing scope must be final or effectively final
i am trying to read how many rows are there on the location (i) and display separated by enter which one is displayed at the moment(j) - example 1 of 15 when pressed enter will display 2 of 15 here's part of code which causes error:我正在尝试读取位置 (i) 上有多少行,并通过 Enter 分隔显示当前显示的行 (j) - 按下 Enter 时 15 个示例中的 1 个将显示 15 个中的 2 个,这是导致代码的一部分错误:
int i = 0;
int j = 0;
try {
ResultSet rs = stmt.executeQuery("SELECT * FROM product WHERE Location = 'REC1U'");
while(rs.next()) {
i++;
}
rs.next();
tfield.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
j++; // <--- this is causing above error
String l = String.valueOf(j);
count.setText(l);
}
}
});
} finally {
stmt.close();
}
i have tried to make it final just to try to get rid of it but it wouldn't work anyways...我试图让它成为最终的只是为了摆脱它,但无论如何它都行不通......
You can use an object that has a counter inside which is final.您可以使用内部具有最终计数器的对象。 For example:例如:
final AtomicInteger j = new AtomicInteger(0);
//...
public void keyPressed(KeyEvent e) {
j.incrementAndGet();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.