Asgard
Asgard

Reputation: 388

Strangely equals returns always false also if the values seems match

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??

Upvotes: 2

Views: 2881

Answers (3)

Kaden
Kaden

Reputation: 81

As stated by the above, but with the corrected method:

public int getParam(String paramName){
    String param = paramName.trim();
    if(param.equalsIgnoreCaseIgnoreCase("Balls and net")){
        return 1;
    }
    else if(param.equalsIgnoreCase("Balls and bat")){
        return 2;
    }
    else if(param.equalsIgnoreCase("Without balls")){
        return 3;
    }
    else if(param.equalsIgnoreCase("Team Sport")){
        return 4;
    }
    return -1;
}

It always helps to remember that equals is case sensitive. equalsIgnoreCase is the alternative to this. This was a very common mistake I made when I was a beginner at java.

you can also use contains(), but remember to be careful with what you are comparing your string to with this method.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

Most likely you have trailing or leading whitespaces. trim() first:

public int getParam(String rawParamName){
  final String paramName = rawParamName.trim();

  if(paramName.equalsIgnoreCase("Balls and net")){
  //...

Also watch out for character case. Team sport will 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.

Upvotes: 3

Bozho
Bozho

Reputation: 597076

  • .trim() before comparison
  • .equalsIgnoreCase(..)

=>

if (paramName().trim().equalsIgnoreCase("..")) { .. }

Upvotes: 2

Related Questions