Tengo una duda con este código en java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Tengo una duda con este código en java

public Contacto buscaPorHashtag( String hashtag ) { Set<String> claves = tabla.keySet(); for(String i: claves){ String valor = tabla.get(i); if((hashtag.equals(valor))){ Contacto persona = new Contacto(i,hashtag); return persona; } } } Es un método de un clase de Java y lo que ocurre es q no me está ingresando al if y no me reconoce el return. He importado las librerías hashmap y set para que funcione. El objetivo de la función es tratar de imprimir la clave a partir del valor hashtag ingresado como parametro

14th Jun 2019, 10:03 PM
edgar andres
edgar andres - avatar
1 Answer
+ 1
//this works for me: public class Contactos { Hashtable<String,String> tabla; Contacto buscaPorHashtag( String hashtag ) { Contacto persona = null; Set<String> claves = tabla.keySet(); for (String i: claves) { String valor = tabla.get(i); if (( hashtag.equals(valor) )) { persona = new Contacto(i,hashtag); break; } } return persona; } } here is not: - imports - class Contacto with (name, hash) fields for returning object - class Test with main() where I create Contactos object and add elements to HashTable tabla. + calling search method from Test.main() + print the result
15th Jun 2019, 12:28 AM
zemiak