M
M
Maks Burkov2016-11-21 20:34:35
Java
Maks Burkov, 2016-11-21 20:34:35

Java Iterator, what am I doing wrong?

@Override
    public Map<Class, Object> JSONFormatStringSeparator(JSONObject jsonObject) {
       Iterator<String> keys = jsonObject.keys(); // ключи которые я передаю в 2 метода. 
        while (keys.hasNext()){
           String key = keys.next();
           System.out.print(": "+key); // выводятся все! 
       }
       String value = valueChecker(keys,jsonObject);
        if(value!=null) {
            switch (value) {
                case "create-company":
                    String [] array = {"id","compname","password","email"};
                    if(keyChecker(keys,array) == array.length){
                        System.out.println("All found"); // для проверки
                        Company company = new Company(jsonObject.getLong("id"),jsonObject.getString("compname"),jsonObject.getString("password"),jsonObject.getString("email"));
                        return objectMapper(Company.class,company);
                    }
                }
            }

        return null;
    }

private String valueChecker(Iterator<String> keys,JSONObject jsonObject){
        while(keys.hasNext()){
            String key =  keys.next();
            System.out.print(key+":From value checker "); // выводится только formId 
            if(key.equals("formId")){
              return (String)jsonObject.get(key);
            }
        }
        return null;
    }

// в данном методе выводится всё кроме formId
 private byte keyChecker(Iterator<String>keys,String[]values){
        byte index = 0,matches = 0;
        while (keys.hasNext()){
            String key =  keys.next();
            System.out.println(key); // вывод на проверку. 
            if(!(key.equals("formId"))) {
                if (key.equals(values[index])) {
                    matches++;
                }
            }
               index++;
            }
        return matches;
    }

According to the given code logic. How can it be that in one method only one of the keys is displayed, and in the other all but one ?
jsonobject
"id":"***",
  "compname":"***",
  "password":"***",
  "email":"***",
  "formId":"create-company"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-11-21
@Maks00088

en.stackoverflow.com/a/594068/177947

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question