Hello,
I am having a problem with a program that is supposed to monitor correct and incorrect keys typed. The code is below. The problem seems to lie in a else statement within method keytyped. If I comment this else statement out, the program will compile(but not work as I intend it to.
import java.util.Vector;
import java.awt.;
import java.awt.event.;
import javax.swing.;
import java.io.;
import java.awt.BorderLayout;
import java.awt.FlowLayout; import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JTextField;
import java.util.*;
public class DataEntry extends JFrame implements FocusListener,KeyListener
{
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JPanel Panel1;
private JPanel Panel2;
private JPanel Panel3;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private BorderLayout layout;
private GridLayout gridLayout1;
private FlowLayout flowLayout1;
private String a;
private String b;
private String c;
private String d;
private String l;
private int f;
private int v;
private int x;
private int y;
private int z;
private int n;
private int correct=0;
private int array[]=new int[12];
private int incorrect=0;
private int counter2=-1;
Formatter formatter=new Formatter();
Formatter formatter2=new Formatter();
Formatter formatter3=new Formatter();
public DataEntry()
{
super("Data Entry");
layout = new BorderLayout(5,10);
flowLayout1= new FlowLayout();
setLayout(layout);
Panel1 = new JPanel();
Panel2=new JPanel();
Panel3=new JPanel();
Random randomNumber=new Random();
for(int counter=0;counter<array.length;counter++){
array[counter]=randomNumber.nextInt(10);
}
formatter.format("%d%d%d%d",array[0],array[1],array[2],array[3]);
formatter2.format("%d%d%d%d",array[4],array[5],array[6],array[7]);
formatter3.format("%d%d%d%d",array[8],array[9],array[10],array[11]);
//d=String.valueOf(z);
label1=new JLabel(formatter.toString());
label2=new JLabel(formatter2.toString());
label3=new JLabel(formatter3.toString());
label4=new JLabel("");
add(Panel1,BorderLayout.NORTH);
Panel1.setLayout(flowLayout1);
Panel1.add(label1);
Panel1.add(label2);
Panel1.add(label3);
add(Panel2,BorderLayout.SOUTH);
textField2=new JTextField(4);
textField3=new JTextField(4);
textField1=new JTextField(4);
textField1.addFocusListener(this);
textField3.addFocusListener(this);
textField2.addFocusListener(this);
textField1.setName("David");
textField1.addKeyListener(this);
textField2.addKeyListener(this);
textField3.addKeyListener(this);
textField2.setName("John");
textField3.setName("Roger");
Panel2.add(textField1);
Panel2.add(textField2);
Panel2.add(textField3);
add(Panel3,BorderLayout.CENTER);
Panel3.setLayout(flowLayout1);
Panel3.add(label4);
}
public void focusGained(FocusEvent e) {
System.out.println(“focusGained”);
if(e.getComponent() instanceof JTextField) {
JTextField jTextField = (JTextField) e.getComponent();
if(jTextField.getName() != null && jTextField.getName().equals(“David”)) {
label4.setText(formatter.toString());
}
if(jTextField.getName() != null && jTextField.getName().equals(“John”)) {
label4.setText(formatter2.toString());
}
if(jTextField.getName() != null && jTextField.getName().equals(“Roger”)) {
label4.setText(formatter3.toString());
}
}
}
public void focusLost(FocusEvent e) {
}
public void keyTyped(KeyEvent e) {
l=String.format("%s",e.getKeyChar());
counter2++;
System.out.printf("%s",l);
System.out.printf("%d",array[counter2]);
b=String.valueOf(array[counter2]);
System.out.printf("%s",b);
if(l.equals(b)){
correct++;
System.out.printf("Correct:%d\
Incorrect:%d
",correct,incorrect);
}
else { {
incorrect++;
System.out.printf("Correct:%d\
Incorrect:%d
",correct,incorrect);
}
f++;
if(f==4){
textField2.requestFocus();
}
if(f==8){
textField3.requestFocus();
}
if(f==12){
f=0;
textField1.requestFocus();
}
}
/** Handle the key-pressed event from the text field. */
public void keyPressed(KeyEvent e) {
}
/** Handle the key-released event from the text field. */
public void keyReleased(KeyEvent e) {
}
}