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
– user13288315
There is something I can edit in my question to facilitate your assistance ?
– user13288315
Man, thank you so much for solving my question
– user13288315