Problem comparing java string

Asked

Viewed 50 times

0

I am new to developing using java and came across the following error: When giving the Return inside my if that is inside the java is the following error:"Missing Return statement" The big question is that I need exactly my content that I return within the if.

private int constructor(String key){
        String format = new FormatString().Format(key);
        for(int i = 0; i < this.keysOfMap.length; i++){

            for(int k = 0; k < this.valuesOfKey[i].length; k++){
                System.out.println(i);
                  if(format == this.valuesOfKey[i][k]){
                      return  i;
                  }
              }
        }
    };

Another problem is that it is not falling inside my if, because the string comparison is returning false even though both are the same thing for ex:

    if("ola" == "ola")
        //true
    
    if(format == 'subtrair')
       //false
       //no meu format está chegando literalmente a palavra subtrair e mesmo assim ele retorna false;

    if(format == this.valuesOfKey[i][k])
       //false
       //aqui ele tbm retorna false, mesmo tendo dentro do meu array a palavra "subtrair"
      
      //eu usei o .getClass().getSimpleName() em ambos os valores sendo comparados e todos são do tipo String;

I really don’t know what to do if you can give me a north

4 answers

1


Hello,

All right, let’s split up.

About the return, every Java method that declares a return type must have a return, in this case this code below only returns something if it enters within the is, Java understands that if this.keysOfMalength is equal to 0 (i.e., if there are no elements) it does not enter the for and therefore does not return value.

private int constructor(String key){
    String format = new FormatString().Format(key);
    for(int i = 0; i < this.keysOfMap.length; i++){
        for(int k = 0; k < this.valuesOfKey[i].length; k++){
            System.out.println(i);
              if(format == this.valuesOfKey[i][k]){
                  return  i;
              }
          }
    }
};

One way to fix this is to return an Optional or return a type that accepts reference as an Integer, for example:

private Integer constructor(String key){
    String format = new FormatString().Format(key);
    for(int i = 0; i < this.keysOfMap.length; i++){
        for(int k = 0; k < this.valuesOfKey[i].length; k++){
            System.out.println(i);
              if(format == this.valuesOfKey[i][k]){
                  return  i;
              }
          }
    }
    return null;
};

In this case if this.keysOfMap has no elements the method returns null. For this it was necessary to change the return type from int to Integer, since int is a primitive it cannot be null.

About String comparison, strings in Java are objects, when you make a comparison like:

String nome1 = "Rafael";
String nome2 = "Rafael";
System.out.print(nome1 == nome2)

What is being compared is whether the String object name1 is equal to the String object Nome2.

To compare the contents of the object, i.e., the "Rafael" strings of the String class have some methods such as equals and equalsIgnoreCase;

String nome1 = "Rafael";
String nome2 = "rafael";
nome1.equals(nome2); // false porque Rafael != rafael
nome1.equalsIgnoreCase(nome2); // true porque ignora maíusculas de minusculas na comparação.

If you need to compare your objects by their values you can implement methods like equals in them that make this comparison.

It is important at this point to also understand what it is to be equal, and why Java does this.

Some "design patterns" have different equal definitions.

Entity (entity) understands that two objects are the same object if their identifier is equal, for example, if you have two objects and both have an identifier called "id" and their value is equal then by Entity it is understood that they are the same object.

In Value Object (value object) it understands that one object is equal to another when all properties have the same value.

In this case what counts is the intention (of the code, what the code wants/should do), not always objects should be equal because it has the same value, and Java is one of the languages (as many others) that allows this type of behavior.

  • Good morning ooredroxo, I really liked your resolvi and the error of return, however I still have error in comparing my strings

  • There is something I can edit in my question to facilitate your assistance ?

  • 1

    Man, thank you so much for solving my question

0

-1

Good night,

I am at the beginning of the Java course, but I went through this case a little while ago.

When trying to compare two objects in Java without implementing the equals method, the return will be the same as matching two objects, because what will be returned is the memory address of that object and not its content.

To be compared the contents of the strings need to be implemented the equals method before calling the function.

-1

for the first problem where you have the error -> "Missing Return statement" plus returns i inside if, at the end of the block before you close the method also need to return some value, in which case I suggest you do a Return -1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.