I tried it on my own, then compared my approach with the ones in the forums but it still doesn't validate. Output is as required. Can anyone help please?
package com.codegym.task.task22.task2208;
import java.util.Map;
import java.util.HashMap;
import java.util.Formatter;
public class Solution {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "Johnson");
map.put("country", "United States");
map.put("city", "Los Angeles");
map.put("age", null);
System.out.println(getQuery(map));
}
public static String getQuery(Map<String, String> params) {
String[] arr = {"name", "country", "city", "age"};
StringBuilder sb = new StringBuilder();
Formatter fm = new Formatter(sb);
int i = 0;
for (String s : arr) {
for(Map.Entry<String, String> me : params.entrySet()){
if (me.getKey().equalsIgnoreCase(arr[i])&& me.getValue()!=null){
fm.format("%s = '%s'",arr[i], me.getValue());
i++;
if(i != params.size()-1) {
sb.append(" and ");
}
}
}
}
return sb.toString();
}
}