I have this methods
getParam
public int getParam(String paramName){
if(paramName.equals("Balls and net")){
expressionParam=1;
}
else if(paramName.equals("Balls and bat")){
expressionParam=2;
}
else if(paramName.equals("Without balls")){
expressionParam=3;
}
else if(paramName.equals("Team Sport")){
expressionParam=4;
}
else{
expressionParam=-1;
}
return expressionParam;
}
but for some strange reason the equals in the if conditions returns always false and the method consequentially returns always -1
I invoke this method in the follow button listener
JButton btnNewButton = new JButton(" OK ");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Object[][] matrix=(((MyTableModel) table.getModel()).getDatamatrix());
List<Expression> list=new LinkedList<Expression>();
for (int i = 0; i <elem; i++){
Expression e=new Expression(getSport((matrix[i][0]).toString()), getParam((matrix[i][1]).toString()), getSport((matrix[i][2]).toString()));
list.add(v);
}catch...
and with a println I have verified that the values passed to getParams match.
So what is wrong with this method??
Most likely you have trailing or leading whitespaces.
trim()first:Also watch out for character case.
Team sportwill actually return-1. Consider usingequalsIgnoreCase()instead.Finally you might have different number or type of white spaces, e.g. two spaces or tab between character. This requires a little bit more work.
Finally if you want to be extra flexible consider using levenshtein distance.