If-else key event problem

Hi, I was hoping to get some help on a problem with this class. Below is the handler for a bunch of text boxes, and its definitly getting each score properly , yet the event always calls the else part of the last statement. Is there something wrong with the way I've implemented the counter? :S

class SecondRoundScoresHandlerimplements KeyListener

{

private KnockOutStages kOS;

private String[] scoresArray;

String winner1code;

String winner1name;

String winner2code;

String winner2name;

String winner3code;

String winner3name;

String winner4code;

String winner4name;

String winner5code;

String winner5name;

String winner6code;

String winner6name;

String winner7code;

String winner7name;

String winner8code;

String winner8name;

String[] winner1=new String[2];

String[] winner2=new String[2];

String[] winner3=new String[2];

String[] winner4=new String[2];

String[] winner5=new String[2];

String[] winner6=new String[2];

String[] winner7=new String[2];

String[] winner8=new String[2];

int counter = 0;

public SecondRoundScoresHandler(KnockOutStages kOS)

{

this.kOS=kOS;

}

// unused

publicvoid keyTyped(KeyEvent e){}

//unused

publicvoid keyPressed(KeyEvent e){}

//The listener method defined in the interface

publicvoid keyReleased(KeyEvent e)

{

if (e.getKeyCode() == KeyEvent.VK_ENTER)

{

GroupsReadIn gRI =new GroupsReadIn();

//Sets winners of each second round match to be passed to the quarter finals

//Match 1

if(kOS.scoreAwayMatch1.getText().equals(kOS.scoreHomeMatch1.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch1.getText())>Integer.parseInt(kOS.scoreAwayMatch1.getText()))

{

gRI.readInSecondRound("groupAwinner.txt");

winner1[0] = gRI.secondRoundTeamCode();

winner1[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupBrunnerup.txt");

winner1[0] = gRI.secondRoundTeamCode();

winner1[1] = gRI.secondRoundTeamName();

}

//Match 2

if(kOS.scoreAwayMatch2.getText().equals(kOS.scoreHomeMatch2.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch2.getText())>Integer.parseInt(kOS.scoreAwayMatch2.getText()))

{

gRI.readInSecondRound("groupBwinner.txt");

winner2[0] = gRI.secondRoundTeamCode();

winner2[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupCrunnerup.txt");

winner2[0] = gRI.secondRoundTeamCode();

winner2[1] = gRI.secondRoundTeamName();

}

//Match 3

if(kOS.scoreAwayMatch3.getText().equals(kOS.scoreHomeMatch3.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch3.getText())>Integer.parseInt(kOS.scoreAwayMatch3.getText()))

{

gRI.readInSecondRound("groupCwinner.txt");

winner3[0] = gRI.secondRoundTeamCode();

winner3[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupDrunnerup.txt");

winner3[0] = gRI.secondRoundTeamCode();

winner3[1] = gRI.secondRoundTeamName();

}

//Match 4

if(kOS.scoreAwayMatch4.getText().equals(kOS.scoreHomeMatch4.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch4.getText())>Integer.parseInt(kOS.scoreAwayMatch4.getText()))

{

gRI.readInSecondRound("groupDwinner.txt");

winner4[0] = gRI.secondRoundTeamCode();

winner4[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupErunnerup.txt");

winner4[0] = gRI.secondRoundTeamCode();

winner4[1] = gRI.secondRoundTeamName();

}

//Match 5

if(kOS.scoreAwayMatch5.getText().equals(kOS.scoreHomeMatch5.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch5.getText())>Integer.parseInt(kOS.scoreAwayMatch5.getText()))

{

gRI.readInSecondRound("groupEwinner.txt");

winner5[0] = gRI.secondRoundTeamCode();

winner5[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupFrunnerup.txt");

winner5[0] = gRI.secondRoundTeamCode();

winner5[1] = gRI.secondRoundTeamName();

}

//Match 6

if(kOS.scoreAwayMatch6.getText().equals(kOS.scoreHomeMatch6.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch6.getText())>Integer.parseInt(kOS.scoreAwayMatch6.getText()))

{

gRI.readInSecondRound("groupFwinner.txt");

winner6[0] = gRI.secondRoundTeamCode();

winner6[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupGrunnerup.txt");

winner6[0] = gRI.secondRoundTeamCode();

winner6[1] = gRI.secondRoundTeamName();

}

//Match 7

if(kOS.scoreAwayMatch7.getText().equals(kOS.scoreHomeMatch7.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch7.getText())>Integer.parseInt(kOS.scoreAwayMatch7.getText()))

{

gRI.readInSecondRound("groupGwinner.txt");

winner7[0] = gRI.secondRoundTeamCode();

winner7[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupHrunnerup.txt");

winner7[0] = gRI.secondRoundTeamCode();

winner7[1] = gRI.secondRoundTeamName();

}

//Match 8

if(kOS.scoreAwayMatch8.getText().equals(kOS.scoreHomeMatch8.getText()))

{

counter++;

}

elseif(Integer.parseInt(kOS.scoreHomeMatch8.getText())>Integer.parseInt(kOS.scoreAwayMatch8.getText()))

{

gRI.readInSecondRound("groupHwinner.txt");

winner8[0] = gRI.secondRoundTeamCode();

winner8[1] = gRI.secondRoundTeamName();

}

else

{

gRI.readInSecondRound("groupArunnerup.txt");

winner8[0] = gRI.secondRoundTeamCode();

winner8[1] = gRI.secondRoundTeamName();

}

if(counter==0)

{

WriteToFile wTF =new WriteToFile();

wTF.finalsKnockout(winner1,"winner1","stage2");

wTF.finalsKnockout(winner2,"winner2","stage2");

wTF.finalsKnockout(winner3,"winner3","stage2");

wTF.finalsKnockout(winner4,"winner4","stage2");

wTF.finalsKnockout(winner5,"winner5","stage2");

wTF.finalsKnockout(winner6,"winner6","stage2");

wTF.finalsKnockout(winner7,"winner7","stage2");

wTF.finalsKnockout(winner8,"winner8","stage2");

JOptionPane.showMessageDialog(null, winner1[1]+"\n"+winner2[1]+"\n"+winner3[1]+"\n"+winner4[1]+"\n"+winner5[1]+"\n"+winner6[1]+"\n"+winner7[1]+"\n"+winner8[1]+"\n\nThese teams have qualified\nfor the Quarter Finals.","World Cup 2006", JOptionPane.INFORMATION_MESSAGE,new ImageIcon("images/cup.jpg"));

QuarterFinals qF =new QuarterFinals(winner1,winner2,winner3,winner4,winner5,winner6,winner7,winner8);

qF.getWindow();

}

else

{

JOptionPane.showMessageDialog(null,"Games cannot be drawn at this stage.\nPlease amend scores.","World Cup 2006", JOptionPane.ERROR_MESSAGE,new ImageIcon("images/cup.jpg"));

}

}

}

}

[12661 byte] By [Explosion_Of_Coloura] at [2007-10-2 16:23:41]
# 1

Your code contains a lot of "noise" that makes it hard to find the problem. It's usually considered good practice on these forums to come up with the smallest example that replicates your problem.

As for the code itself, there are a lot of places where counter++ occurs within an if statement before the last one. I can't tell from your code when the guards for those if's are supposed to be true, but from your question I'm assuming they shouldn't be. Try stepping through your program with a debugger to see where the counter gets incemented.

DaanSa at 2007-7-13 17:21:09 > top of Java-index,Security,Event Handling...